帮助Prolog列表 [英] Help With Prolog Lists
问题描述
我是Prolog的新手,正在寻求帮助.我想做的基本上是从列表中获取一些列表,如果这有意义的话?大声笑
Im new to Prolog and was looking for some assistance. What i am trying to do is basically get the some of the list of list, if that makes sense? lol
我想要实现的是... sum([[1,2 ,, [3,4],[5,6]]).应该返回: 列表数:3 清单1 3 清单2 7 清单3 11 ....等
What i am trying to achieve is.... sum([ [1,2],[3,4],[5,6] ]). should return: Number Of Lists: 3 List 1 3 List 2 7 List 3 11....etc
我可以获得很简单的列表数,但是我不太确定如何遍历列表,然后为每个列表加个数.我使事情变得比实际复杂吗?哈哈!
I can get the Number of Lists which is fairly simple but im not quite sure how to loop through the List and then for each List add the number up. Am i making this more complicated than it actually is? lol!
如果有人可以帮助我或向我指出总体方向,那将是很好的.
If anyone can help me or point me in the general direction that would be great.
预先感谢
推荐答案
在SWI-Prolog中,您可以使用maplist
和sumlist
;
In SWI-Prolog you can use maplist
and sumlist
;
?- maplist(sumlist, [[1,2], [3,4], [5,6]], Lengths).
Lengths = [3, 7, 11].
现在,您可以按照自己喜欢的方式漂亮地打印Lengths
.
Now you can pretty-print Lengths
the way you like.
要了解如何实现maplist
和sumlist
,只需调用listing(maplist)
和listing(sumlist)
.
To learn how maplist
and sumlist
are implemented, just call listing(maplist)
and listing(sumlist)
.
这篇关于帮助Prolog列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!