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. h5自动生成工具

    一.前言 写了很多h5之后,对于写手写html和css已经麻木的我决定动手写个工具自动生成h5结构和样式.其实这个想法由来已久,但总是觉得自己技术不够,所以一直没实行.直到某天我真的写够了,我决定动手 ...

  2. 仿SiteMap实现Asp.net 网站的菜单和权限管理

    在Asp.net中,SiteMap用于站点导航,可以与Menu等控件一起使用实现网站的菜单和权限管理.但是SiteMap提供的方法都是只读的,无法再运行时修改(菜单)导航文件,需要手动修改配置web. ...

  3. Debian修改ssh端口和禁止root远程登陆设置

    linux修改端口22vi /etc/ssh/sshd_config找到#port 22将前面的#去掉,然后修改端口 port 1234重启服务就OK了service sshd restart或/et ...

  4. Session为null无法访问

    我们在一般处理程序中需要访问Session为null 无法访问和操作 处理方案: 1.导入命名空间  System.Web.SessionState 2.实现IRequiresSessionState ...

  5. 团队项目--站立会议 DAY3

    小组名称:D&M 参会人员:张靖颜,钟灵毓秀,何玥,赵莹,王梓萱 今天是站立会议的第三天 在前两次会议的基础上 组员们总结了自己在任务中的经验 个抒己论在会议中进行了一些讨论 并且对接下来的工 ...

  6. 杂记 C中的volatile

    volatile 就象大家更熟悉的const一样,volatile是一个类型修饰符(type specifier).它是被设计用来修饰被不同线程访问和修改的变量.如果没有volatile,基本上会导致 ...

  7. 使用grunt打包ueditor源代码

    支持版本支持 UEditor 1.3.0+ 的版本 使用方法1.线上下载ueditor下载地址:ueditor,要下载"完整版 + 源码" 2.安装nodejs下载nodejs并安 ...

  8. ios 项目里常用的宏

    NSLog(@"__func__ :  %s", __func__);//oc测试环境,打印文件名,方法名 NSLog(@"__FUNCTION__ : %s" ...

  9. crossplatform---Nodejs in Visual Studio Code 07.学习Oracle

    1.开始 Node.js:https://nodejs.org OracleDB: https://github.com/oracle/node-oracledb/blob/master/INSTAL ...

  10. atitit.文件上传带进度条的实现原理and组件选型and最佳实践总结O7

    atitit.文件上传带进度条的实现原理and组件选型and最佳实践总结O7 1. 实现原理 1 2. 大的文件上传原理::使用applet 1 3. 新的bp 2 1. 性能提升---分割小文件上传 ...