Prolog 人工智能语言中文论坛---打造优质Prolog学习交流园地

一个供Prolog爱好者学习与交流的地方


您没有登录。 请登录注册

prolog学习记录,长贴

浏览上一个主题 浏览下一个主题 向下  留言 [第1页/共1页]

1 prolog学习记录,长贴 于 周四 八月 16, 2012 2:55 pm

prolog(大坑(三基友(1))).

​ 某日遇到一个逻辑测试题,大意如下:三个人来自辽宁,湖南,重庆,其中李浩与重庆人不同岁,张翔比辽宁人小,重庆人比王明大。求三个人分别来自哪里,及年龄排序。
人肉解法很简单,重庆人既不是李浩也不是王明,所以重庆人是张翔;
王明小于(张翔,重庆),(张翔,重庆)小于辽宁人,所以王明是湖南人,且年龄最小;
最后推出李浩是辽宁人,年龄最大。

据说prolog可以轻松解出这种条件约束的问题,包括比这个更复杂的谁养斑马的问题,于是赶紧下书来读,下面是惨不忍睹的坎坷道路。
bigger(大,小).这个谓词不会编,所以只能先列出全排列,然后再从中做出筛选。
全排列代码如下:
name(lihao).
name(wangming).
name(zhangxiang).
from(liaoning).
from(hunan).
from(chongqing).
pailie(List):-
List=(p(N1,F1),p(N2,F2),p(N3,F3)),
name(N1),name(N2),name(N3),
not(N1=N2;N2=N3;N1=N3),
from(F1),from(F2),from(F3),
not(F1=F2;F2=F3;F1=F3),
write(List),nl,fail.
**************************************
运行?-pailie(List). 结果如下:
p(lihao,liaoning),p(wangming,hunan),p(zhangxiang,chongqing)
p(lihao,liaoning),p(wangming,chongqing),p(zhangxiang,hunan)
p(lihao,hunan),p(wangming,liaoning),p(zhangxiang,chongqing)
p(lihao,hunan),p(wangming,chongqing),p(zhangxiang,liaoning)
p(lihao,chongqing),p(wangming,liaoning),p(zhangxiang,hunan)
p(lihao,chongqing),p(wangming,hunan),p(zhangxiang,liaoning)
p(lihao,liaoning),p(zhangxiang,hunan),p(wangming,chongqing)
p(lihao,liaoning),p(zhangxiang,chongqing),p(wangming,hunan)
p(lihao,hunan),p(zhangxiang,liaoning),p(wangming,chongqing)
p(lihao,hunan),p(zhangxiang,chongqing),p(wangming,liaoning)
p(lihao,chongqing),p(zhangxiang,liaoning),p(wangming,hunan)
p(lihao,chongqing),p(zhangxiang,hunan),p(wangming,liaoning)
p(wangming,liaoning),p(lihao,hunan),p(zhangxiang,chongqing)
p(wangming,liaoning),p(lihao,chongqing),p(zhangxiang,hunan)
p(wangming,hunan),p(lihao,liaoning),p(zhangxiang,chongqing)
p(wangming,hunan),p(lihao,chongqing),p(zhangxiang,liaoning)
p(wangming,chongqing),p(lihao,liaoning),p(zhangxiang,hunan)
p(wangming,chongqing),p(lihao,hunan),p(zhangxiang,liaoning)
p(wangming,liaoning),p(zhangxiang,hunan),p(lihao,chongqing)
p(wangming,liaoning),p(zhangxiang,chongqing),p(lihao,hunan)
p(wangming,hunan),p(zhangxiang,liaoning),p(lihao,chongqing)
p(wangming,hunan),p(zhangxiang,chongqing),p(lihao,liaoning)
p(wangming,chongqing),p(zhangxiang,liaoning),p(lihao,hunan)
p(wangming,chongqing),p(zhangxiang,hunan),p(lihao,liaoning)
p(zhangxiang,liaoning),p(lihao,hunan),p(wangming,chongqing)
p(zhangxiang,liaoning),p(lihao,chongqing),p(wangming,hunan)
p(zhangxiang,hunan),p(lihao,liaoning),p(wangming,chongqing)
p(zhangxiang,hunan),p(lihao,chongqing),p(wangming,liaoning)
p(zhangxiang,chongqing),p(lihao,liaoning),p(wangming,hunan)
p(zhangxiang,chongqing),p(lihao,hunan),p(wangming,liaoning)
p(zhangxiang,liaoning),p(wangming,hunan),p(lihao,chongqing)
p(zhangxiang,liaoning),p(wangming,chongqing),p(lihao,hunan)
p(zhangxiang,hunan),p(wangming,liaoning),p(lihao,chongqing)
p(zhangxiang,hunan),p(wangming,chongqing),p(lihao,liaoning)
p(zhangxiang,chongqing),p(wangming,liaoning),p(lihao,hunan)
p(zhangxiang,chongqing),p(wangming,hunan),p(lihao,liaoning)
false.

查阅用户资料

2 回复: prolog学习记录,长贴 于 周四 八月 16, 2012 3:05 pm

在想出bigger/2这个谓词前,还有一个小插曲(挫折):其他语言中可以用来计数的n=n+1,在prolog中怎样做才能得到下面这种效果呢

1 p(lihao,liaoning),p(wangming,hunan),p(zhangxiang,chongqing)
2 p(lihao,liaoning),p(wangming,chongqing),p(zhangxiang,hunan)
3 p(lihao,hunan),p(wangming,liaoning),p(zhangxiang,chongqing)
4 p(lihao,hunan),p(wangming,chongqing),p(zhangxiang,liaoning)
5 p(lihao,chongqing),p(wangming,liaoning),p(zhangxiang,hunan)
6 p(lihao,chongqing),p(wangming,hunan),p(zhangxiang,liaoning)
7 p(lihao,liaoning),p(zhangxiang,hunan),p(wangming,chongqing)
8 p(lihao,liaoning),p(zhangxiang,chongqing),p(wangming,hunan)
9 p(lihao,hunan),p(zhangxiang,liaoning),p(wangming,chongqing)
10 p(lihao,hunan),p(zhangxiang,chongqing),p(wangming,liaoning)
11 p(lihao,chongqing),p(zhangxiang,liaoning),p(wangming,hunan)
12 p(lihao,chongqing),p(zhangxiang,hunan),p(wangming,liaoning)
13 p(wangming,liaoning),p(lihao,hunan),p(zhangxiang,chongqing)
14 p(wangming,liaoning),p(lihao,chongqing),p(zhangxiang,hunan)
15 p(wangming,hunan),p(lihao,liaoning),p(zhangxiang,chongqing)
16 p(wangming,hunan),p(lihao,chongqing),p(zhangxiang,liaoning)
17 p(wangming,chongqing),p(lihao,liaoning),p(zhangxiang,hunan)
18 p(wangming,chongqing),p(lihao,hunan),p(zhangxiang,liaoning)
19 p(wangming,liaoning),p(zhangxiang,hunan),p(lihao,chongqing)
20 p(wangming,liaoning),p(zhangxiang,chongqing),p(lihao,hunan)
21 p(wangming,hunan),p(zhangxiang,liaoning),p(lihao,chongqing)
22 p(wangming,hunan),p(zhangxiang,chongqing),p(lihao,liaoning)
23 p(wangming,chongqing),p(zhangxiang,liaoning),p(lihao,hunan)
24 p(wangming,chongqing),p(zhangxiang,hunan),p(lihao,liaoning)
25 p(zhangxiang,liaoning),p(lihao,hunan),p(wangming,chongqing)
26 p(zhangxiang,liaoning),p(lihao,chongqing),p(wangming,hunan)
27 p(zhangxiang,hunan),p(lihao,liaoning),p(wangming,chongqing)
28 p(zhangxiang,hunan),p(lihao,chongqing),p(wangming,liaoning)
29 p(zhangxiang,chongqing),p(lihao,liaoning),p(wangming,hunan)
30 p(zhangxiang,chongqing),p(lihao,hunan),p(wangming,liaoning)
31 p(zhangxiang,liaoning),p(wangming,hunan),p(lihao,chongqing)
32 p(zhangxiang,liaoning),p(wangming,chongqing),p(lihao,hunan)
33 p(zhangxiang,hunan),p(wangming,liaoning),p(lihao,chongqing)
34 p(zhangxiang,hunan),p(wangming,chongqing),p(lihao,liaoning)
35 p(zhangxiang,chongqing),p(wangming,liaoning),p(lihao,hunan)
36 p(zhangxiang,chongqing),p(wangming,hunan),p(lihao,liaoning)

查阅用户资料

3 回复: prolog学习记录,长贴 于 周日 八月 19, 2012 2:36 pm

fuyunv 写道::在想出bigger/2这个谓词前,还有一个小插曲(挫折):其他语言中可以用来计数的n=n+1,在prolog中怎样做才能得到下面这种效果呢

1 p(lihao,liaoning),p(wangming,hunan),p(zhangxiang,chongqing)
2 p(lihao,liaoning),p(wangming,chongqing),p(zhangxiang,hunan)
3 p(lihao,hunan),p(wangming,liaoning),p(zhangxiang,chongqing)
4 p(lihao,hunan),p(wangming,chongqing),p(zhangxiang,liaoning)
5 p(lihao,chongqing),p(wangming,liaoning),p(zhangxiang,hunan)
6 p(lihao,chongqing),p(wangming,hunan),p(zhangxiang,liaoning)
7 p(lihao,liaoning),p(zhangxiang,hunan),p(wangming,chongqing)
8 p(lihao,liaoning),p(zhangxiang,chongqing),p(wangming,hunan)
9 p(lihao,hunan),p(zhangxiang,liaoning),p(wangming,chongqing)
10 p(lihao,hunan),p(zhangxiang,chongqing),p(wangming,liaoning)
11 p(lihao,chongqing),p(zhangxiang,liaoning),p(wangming,hunan)
12 p(lihao,chongqing),p(zhangxiang,hunan),p(wangming,liaoning)
13 p(wangming,liaoning),p(lihao,hunan),p(zhangxiang,chongqing)
14 p(wangming,liaoning),p(lihao,chongqing),p(zhangxiang,hunan)
15 p(wangming,hunan),p(lihao,liaoning),p(zhangxiang,chongqing)
16 p(wangming,hunan),p(lihao,chongqing),p(zhangxiang,liaoning)
17 p(wangming,chongqing),p(lihao,liaoning),p(zhangxiang,hunan)
18 p(wangming,chongqing),p(lihao,hunan),p(zhangxiang,liaoning)
19 p(wangming,liaoning),p(zhangxiang,hunan),p(lihao,chongqing)
20 p(wangming,liaoning),p(zhangxiang,chongqing),p(lihao,hunan)
21 p(wangming,hunan),p(zhangxiang,liaoning),p(lihao,chongqing)
22 p(wangming,hunan),p(zhangxiang,chongqing),p(lihao,liaoning)
23 p(wangming,chongqing),p(zhangxiang,liaoning),p(lihao,hunan)
24 p(wangming,chongqing),p(zhangxiang,hunan),p(lihao,liaoning)
25 p(zhangxiang,liaoning),p(lihao,hunan),p(wangming,chongqing)
26 p(zhangxiang,liaoning),p(lihao,chongqing),p(wangming,hunan)
27 p(zhangxiang,hunan),p(lihao,liaoning),p(wangming,chongqing)
28 p(zhangxiang,hunan),p(lihao,chongqing),p(wangming,liaoning)
29 p(zhangxiang,chongqing),p(lihao,liaoning),p(wangming,hunan)
30 p(zhangxiang,chongqing),p(lihao,hunan),p(wangming,liaoning)
31 p(zhangxiang,liaoning),p(wangming,hunan),p(lihao,chongqing)
32 p(zhangxiang,liaoning),p(wangming,chongqing),p(lihao,hunan)
33 p(zhangxiang,hunan),p(wangming,liaoning),p(lihao,chongqing)
34 p(zhangxiang,hunan),p(wangming,chongqing),p(lihao,liaoning)
35 p(zhangxiang,chongqing),p(wangming,liaoning),p(lihao,hunan)
36 p(zhangxiang,chongqing),p(wangming,hunan),p(lihao,liaoning)

有两种方法,
如果36行一次连续印出建议用第一种,如果印出时间点不定,建议用第二种。

第一种:

编写write_many(Count)谓词如下:

代码:
write_many(Count) :- Count =< 36, write(Count), write(' '), .........., Next_Count is Count + 1, write_many(Next_Count).
write_many(_).
"......."的部分是你每次要印的东西。
这个方法的重点是在最后自己调用自己(write_many),然后传进去的参数加记得先加1,
并且在一开始先判断是否小于等于36,大于36则中止,但又不希望大于36之后回传false,
所以下面再加上write_many(_).强制为true。
这样一来,首次调用时写write_many(1),然后它就会自己把数字印到36。


第二种方法用到动态谓词:

先assert(last_count(1)),然后每次write前可以先用last_count(X)来读出现在印到的编号是多少再write出来就行,
write完后要记得先retract(last_count(1)),然后Next_X is X + 1, assert(last_count(Next_X)),
这样子事实才会改成last_count(2).

查阅用户资料 http://prolog.longluntan.net

4 回复: prolog学习记录,长贴 于 周一 八月 20, 2012 6:33 pm


有两种方法,
如果36行一次连续印出建议用第一种,如果印出时间点不定,建议用第二种。

第一种:

编写write_many(Count)谓词如下:

代码:
write_many(Count) :- Count =< 36, write(Count), write(' '), .........., Next_Count is Count + 1, write_many(Next_Count).
write_many(_).
"......."的部分是你每次要印的东西。
这个方法的重点是在最后自己调用自己(write_many),然后传进去的参数加记得先加1,
并且在一开始先判断是否小于等于36,大于36则中止,但又不希望大于36之后回传false,
所以下面再加上write_many(_).强制为true。
这样一来,首次调用时写write_many(1),然后它就会自己把数字印到36。


第二种方法用到动态谓词:

先assert(last_count(1)),然后每次write前可以先用last_count(X)来读出现在印到的编号是多少再write出来就行,
write完后要记得先retract(last_count(1)),然后Next_X is X + 1, assert(last_count(Next_X)),
这样子事实才会改成last_count(2).


多谢liao师,在我没头绪试到各种抓狂的时候,你终于出现了 Laughing bounce

查阅用户资料

浏览上一个主题 浏览下一个主题 返回页首  留言 [第1页/共1页]

您在这个论坛的权限:
不能在这个论坛回复主题