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. Please set registry key HKLM\Microsoft\.NET Framework\InstallRoot to point to the .NET Framework

    安装.NET程序时会提示“Please set registry key HKLM\Microsoft\.NET Framework\InstallRoot to point to the .NET ...

  2. wav文件格式分析(一)

    (一)概述 WAV为微软公司(Microsoft)开发的一种声音文件格式,它符合RIFF(Resource Interchange File Format)文件规范: (二)音频知识 1.常见的声音文 ...

  3. python文件选择:tkFileDialog 基础

    tkFileDialog有两种形式: 一个是.askopenfilename(option=value, ...) 这个是"打开"对话框 另一个是:asksaveasfilenam ...

  4. jquery 编码解码

    中文转Unicode:HttpUtility.UrlEncodeUnicode(string str); 转换后中文格式:"%uxxxx" 举例:"柳_abc123&qu ...

  5. 【Leetcode】【Hard】Merge Intervals

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  6. 第十七章:android解析JSON

    一.解析JSON数据: 首先引入包import org.json.JSONObject;(android sdk 14以后应该自带了 ) Android端的程序解析JSON和JSON数组: packa ...

  7. Android开发学习总结——Android开发的一些相关概念

    一.什么是3G.4G 1995年问世的第一代模拟制式手机(1G)只能进行语音通话. 1996到1997年出现的第二代GSM.CDMA等数字制式手机(2G)便增加了接收数据的功能 Ÿ 3G指的是第三代移 ...

  8. unity3d——自带寻路Navmesh (三)(转)

    继续介绍NavMesh寻路的功能,接下来阿赵打算讲一下以下两个例子,先看看完成的效果:   第一个例子对于喜欢DOTA的朋友应该很熟悉了,就是不同小队分不同路线进攻的寻路,红绿蓝三个队伍分别根据三条路 ...

  9. p4 是否能自动merge

      总结: 1)如果在copy merge(-at)/auto merge(-am)后修改source branch,则可以自动被copy merge: 2)如果在manual merge后修改sou ...

  10. servlet--转向forward与重定向

    当使用forward形式跳转servlet时,地址栏会显示跳转前的servlet访问地址. 跳转是在服务端实现的,客户浏览器并不知道该跳转动作. forward可以跳转到另一个servlet,jsp, ...