在 Prolog 中使用列表列表 [英] Working with list of lists in Prolog
本文介绍了在 Prolog 中使用列表列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请帮助我解决此问题:我有一个列表列表
Please help me to solve this problem: I have a list of lists
[[1,2 ,, [3,4]]
[[1,2],[3,4]]
我如何获得:
[1,3]
[1,4]
[2,3]
[2,4]
或者如果我有一个列表列表
Or if I have a list of lists
[[1,2],[3,4],[6,7]]
[[1,2],[3,4],[6,7]]
我如何获得:
[1,3,6]
[1,3,7]
[1,4,6]
[1,4,7]
[2,3,6]
[2,3,7]
[2,4,6]
[2,4,7]
推荐答案
您可以执行以下操作:
lists([], []).
lists([[Head|_]|Lists], [Head|L]):-
lists(Lists, L).
lists([[_,Head|Tail]|Lists], L):-
lists([[Head|Tail]|Lists], L).
也就是说,采用输入列表中第一个列表的第一个元素,然后递归地处理其余列表.作为第二次机会,跳过该元素并重做其余元素.
That is, take the first element of the first list in your input list and continue recursively with the remaining lists. As a second chance, skip that element and redo with the remaining elements.
这篇关于在 Prolog 中使用列表列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文