[英]Add elements in a list in Prolog
我想从列表中提取元素并将它们添加到另一个新列表中。我怎么能这样做 -
L=[['abc',18],['bcd',19],['def',20]],
nth1(Count,L,List1),
nth1(2,List1,Value),
**NOW I WANT TO PUT THIS Valie in another new list.So finally new list will have
New=[18,19,20]**
如何在新列表中添加元素?
使用地图列表可以实现同样的目的:
?- maplist(nth1(2), [['abc',18],['bcd',19],['def',20]], R).
R = [18, 19, 20].
nth/3
参数的顺序是巧合吗?
假设您的教师希望您自己制定递归解决方案,您可以简单地说出这样的事情(给出您的示例数据):
slurp( [] , [] ) .
slurp( [[_,X]|Xs] , [X|Ys] ) :- slurp(Xs,Ys) .
看到findall / 3和朋友
?- L=[['abc',18],['bcd',19],['def',20]], findall(E,member([_,E],L), R).
L = [[abc, 18], [bcd, 19], [def, 20]],
R = [18, 19, 20].
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.