Symbols in Prolog:
atom
variable
number
list (how to assembly and take them apart)
 
Lists are very powerful in prolog because we do not need to do similar but repeatedtasks again and again. Instead, we can put all elements in a list and deal with them in a more general way.
 
If we have a look at the difference between lists in prolog and arrays in C, it is easy to notice that we do not need to define the length of a list in prolog. Instead, we use arecursive way to deal with them one by one until the remaining is empty.
 
How to write a list:
use commas to separate every element: [A, B]
use vertical bars to divide head and tail: [A | B]
 
The two definitions are quite different
When using [A, B], the list has only 2 element A and B.
When using [A | B], the list can have at least 1 elements because B is a list and it can be empty or very large.
 
How to operate on a list:
Define a stop condition for the recursion;
Deal with lists recursively (always divide the list by head and tail) decreasing the length of it.
 
Examples:
 
example 1: members in a list
 
elem(A, [A | _]).
elem(A, [_ | B]) :- elem(A, B).
 
If we use the following instead, it will only hold when A is the tail of the list.
elem(A, [A]).
elem(A, [_ | B]) :- elem(A, B).
 
 
example 2: list of unique people
 
uniq([ ]).
uniq([H | T]) :- people(H), \+ member(H, T), uniq(T).
 
example 3: joining two lists
 
join([ ], T, T).
join([H | T], L, [H | W]) :- join(T, L, W).
 
We can learn from these examples that we use [H | T] to take the lists into head and tail decreasing the length of it to solve our problem.
 
Using append predicate
append predicate is powerful is because it can analyze the structure of a list.
Notice that the parameter of append predicate must be lists or variables not atoms.
 
append([a, b], [c, d], L).
L=[a, b, c, d].
 
Examples:
 
L1 is start of L2:
front(L1, L2) :- append(L1, _, L2).
 
E is last element of L:
last(E, L) :- append(_, [E], L).
 
another version of member predicate:
mem(A, B) :- append(_, [A | _], B).
 
X is before Y in L:
before(X, Y, L) :- append(Z, [Y | _], L), append(_, [X | _], Z).

Lists in Prolog的更多相关文章

  1. 人工智能技术导论——逻辑程序设计语言PROLOG

    最近在复习人工智能导论,里面介绍了一种逻辑关系语言PROLOG,但这本书里面用到的编译器是Turbo PROLOG,这个编译器早就被淘汰了,我后来找的了它的升级版Visual PROLOG,但一些语法 ...

  2. [LeetCode] Intersection of Two Linked Lists 求两个链表的交点

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  3. [LeetCode] Merge k Sorted Lists 合并k个有序链表

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...

  4. [LeetCode] Merge Two Sorted Lists 混合插入有序链表

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  5. DOM解析XML报错:Content is not allowed in prolog

    报错内容为: Content is not allowed in prolog. Nested exception: Content is not allowed in prolog. 网上所述总结来 ...

  6. 21. Merge Two Sorted Lists —— Python

    题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...

  7. 【leetcode】Intersection of Two Linked Lists

    题目简述: Write a program to find the node at which the intersection of two singly linked lists begins. ...

  8. [LintCode] Intersection of Two Linked Lists 求两个链表的交点

    Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...

  9. [LintCode] Merge Two Sorted Lists 混合插入有序链表

    Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list sh ...

随机推荐

  1. (转)ASP.NET Mvc 2.0 - 1. Areas的创建与执行

    转自:http://www.cnblogs.com/terrysun/archive/2010/04/13/1711218.html ASP.NET Mvc 2.0 - 1. Areas的创建与执行 ...

  2. poj3468 A Simple Problem with Integers (线段树区间最大值)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 92127   ...

  3. Linux内核分析之计算机是如何工作的

    一.计算机工作原理 本周实验主要是反汇编C代码,生成汇编程序.冯·诺依曼理论的要点是:数字计算机的数制采用二进制,计算机应该按照程序顺序执行.人们把冯·诺依曼的这个理论称为冯·诺依曼体系结构.CPU通 ...

  4. UITableView表格视图、UITableView代理方法及应用

    一.基本知识点 UITableView表格视图,是一个可以滚动的界面(理解为垂直滚动的UIScrollView),可展示多行数据,没有行数的限制,只能有一列. 使用UITableView: 1.展示信 ...

  5. SXT_项目

    30. svn服务器运行方式: svnserve:自己做实验的时候用. svn&apache结合起来用.[常用的] 29. EXTJs not Jquery[根据项目组需求] 28. tags ...

  6. nginx+php 安装手册

    http://www.cnblogs.com/hxl2009/archive/2013/06/11/3131627.html  [mysql安装] php 安装 1: wget http://ftp. ...

  7. centos7 安装 notejs

    1.安装集成工具 yum -y install gcc make gcc-c++ 2.安装notejs 自行选择版本:https://nodejs.org/dist/ wget https://nod ...

  8. C语言 数组 行优先 实现

    C语言数组结构行优先顺序存储的实现 (GCC编译). /** * @brief C语言 数组 行优先 实现 * @author wid * @date 2013-11-02 * * @note 若代码 ...

  9. ASP.NET MVC请求处理管道生命周期的19个关键环节(7-12)

    在上一篇"ASP.NET MVC请求处理管道生命周期的19个关键环节(1-6) ",体验了1-6关键环节,本篇继续. ⑦根据IsapiWorkerRequest对象,HttpRun ...

  10. [ACM_其他] 总和不小于S的连续子序列的长度的最小值——尺缩法

    Description: 给定长度为n的整数数列,A[0],A[1],A[2]….A[n-1]以及整数S,求出总和不小于S的连续子序列的长度的最小值.如果解不存在,则输出0. Input: 输入数据有 ...