简体   繁体   English

如何在prolog中显示列表的内容?

[英]How do you show the contents of a list in prolog?

I have the following code: 我有以下代码:

 born(person1,1991).

 born(person2,1965).

 born(person3,1966).

 born(person4,1967).

 born(person5,1968).

 born(person6,1969).


 criteria(X,Y):- born(X,Z) , born(Y,T) , Z<T.
 order([]).  

 order([X]).  

 order([X,Y|L]) :- criteria(X,Y),order([Y|L]). 

I have the predicate order([X,Y|L) that is true if the list is ordered , in this case, the first element should be the oldest person and the last element should be the youngest person. 如果列表是有序的,我有谓词顺序([X,Y | L),在这种情况下,第一个元素应该是最老的人,最后一个元素应该是最年轻的人。

My question is: how would you do a predicate print_List/1 that allows you to print the content of a list . 我的问题是:你如何做一个允许你打印列表内容的谓词print_List / 1。 An example of how it should work would be: 它应该如何工作的一个例子是:

  ?-print_List([X]).
  X = [person2, person3, person4, person5, person6, person1)

Your code it's a bit unusual, it builds a list 'lazily'... 你的代码有点不寻常,它建立一个'懒惰'列表......

?- order(X), write(X).
[]
X = [] ;
[_G357]
X = [_G357] ;
[person2,person1]
X = [person2, person1] ;
[person2,person3]
X = [person2, person3] ;
[person2,person3,person1]
X = [person2, person3, person1] ;
[person2,person3,person4]
X = [person2, person3, person4] .
....

and then a 'all solutions' built in is required, but findall/3 applied to it gives: 然后需要内置'所有解决方案',但是应用了findall / 3可以:

?- findall(X,order(X),L).
L = [[], [_G1108], [person2, person1], [person2, person3], [person2, person3, person1], [person2, person3, person4], [person2, person3|...], [person2|...], [...|...]|...].

You could consider to shorten the code using more directly any of the 'all solutions' built ins. 您可以考虑使用更直接的任何“所有解决方案”内置命令来缩短代码。

Anyway, when write or format don't fit, I use maplist. 无论如何,当写或格式不适合时,我使用maplist。 Paired with library(lambda) you get control in a fairly compact way: for instance, to display your data sorted: 库(lambda)配对,您可以以相当紧凑的方式获得控制:例如,显示您的数据排序:

?- setof(Y-P, Y^P^born(P, Y), L), maplist(writeln, L).
1965-person2
1966-person3
1967-person4
1968-person5
1969-person6
1991-person1
L = [1965-person2, 1966-person3, 1967-person4, 1968-person5, 1969-person6, 1991-person1].

Here setof/3 build a list sorted on Year, then with lambda we can recover the field of interest. 这里setof / 3构建一个按年份排序的列表,然后使用lambda我们可以恢复感兴趣的字段。

?- setof(Y-P, Y^P^born(P, Y), L), maplist(\E^(E=(Y-P), writeln(P)), L).
person2
person3
person4
person5
person6
person1
L = [1965-person2, 1966-person3, 1967-person4, 1968-person5, 1969-person6, 1991-person1].

Shouldn't write/1 do? 不应该write/1吗? Your example doesn't seem to show a print behavior, so you could simply call order with the right parameter (ordered born, either manually or create another predicate to generate it) and the Prolog system should show the content of X. 您的示例似乎没有显示打印行为,因此您可以使用正确的参数调用order(按顺序生成,手动或创建另一个谓词来生成它),Prolog系统应显示X的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM