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. MindFusion Pack for ASP.NET发布v2013.R2

    在MindFusion.Diagramming for WebForms中: 导入OpenOffice Draw文件 新的DrawImporter类允许你通过OpenOffice Draw Vecto ...

  2. mysql增加普通用户后无法登陆问题的解决方法

    解决方法: 增加普通用户后,执行: mysql> use mysql mysql> delete from user where user=''; mysql> flush priv ...

  3. 【基础知识】.Net基础加强06天

    一. 垃圾回收 1. 垃圾回收的目的:提高内存的利用效率. 2. 垃圾回收器: 只回收托管堆中的内存资源,不回收其他资源(数据库连接.文件句柄.网络端口等): 3. 什么时候垃圾回收? a) 当对象没 ...

  4. Kali Linux Web 渗透测试视频教程— 第十六课-拒绝服务攻击

    Kali Linux Web 渗透测试视频教程— 第十六课-拒绝服务攻击 文/玄魂 目录 Kali Linux Web 渗透测试视频教程— 第十六课-拒绝服务攻击................... ...

  5. RSA密钥的跨平台通用

    RSA使用public key加密,用private key解密(签名相反,使用private key签名,用public key验证签名).比如我跟合作方D之间的数据传输,我使用D提供给我的publ ...

  6. macd综合版

    参数设置  SHORE 12    LONG 26    MID 9 DIF:EMA(CLOSE,SHORT)-EMA(CLOSE,LONG); DEA:EMA(DIF,MID),COLOR88888 ...

  7. 股市T+0技巧

    虽然现在股票不能t+0交易了,不过通过股票t+0技巧可以变相的实现t+0交易,尤其在主力方面应用股票t+0技巧更为明显.主力资金一旦介入某股,肯定会建立很大的仓位作为主仓,然后长线持有.然而为了推动股 ...

  8. CSS关于元素垂直居中的问题

    今天碰到了一个问题,给一个父容器和一个子元素,子元素不定高和不定宽,怎么让子元素居中在父容器中,比如下段代码 方法1: <div class="div1"> <d ...

  9. paip. 混合编程的实现resin4 (自带Quercus ) 配置 php 环境

    paip. 混合编程的实现resin4 (自带Quercus ) 配置 php 环境 #---混合编程的类型 1.代码inline 方式 2.使用库/api  解析方式. #----配置resin 支 ...

  10. paip.解决中文url路径的问题图片文件不能显示

    paip.解决中文url路径的问题图片文件不能显示 #现状..中文url路径 图片文件不能显示 <img src="img/QQ截图20140401175433.jpg" w ...