gpt4 book ai didi

list - 展平 Prolog 中的列表列表

转载 作者:行者123 更新时间:2023-12-01 13:52:41 25 4
gpt4 key购买 nike

我有这个列表列表:

 L = [[[a,b],[d,e]],[[m,f],[p,o]],[[r,l],[v,d]]].

我想使用 Prolog 编写一个函数,将其展平成这样:

 L = [[a,b],[c,d],[m,f],[p,o],[r,l],[v,d]].

有什么建议吗?谢谢。

最佳答案

使用 predicate append/2 :

?- L = [[[a,b],[d,e]],[[m,f],[p,o]],[[r,l],[v,d]]], append(L, R).L = [[[a, b], [d, e]], [[m, f], [p, o]], [[r, l], [v, d]]],R = [[a, b], [d, e], [m, f], [p, o], [r, l], [v, d]].

你应该看看at the implementation by SWI-Prolog并在需要时复制它。如果您必须在 GNU-Prolog 中执行此操作,请省略 must_be/2

但是如果你因为 findall/3 需要这个,记住可能还有一个 findall/4 可用(不适用于 GNU-Prolog,但是 SWI -Prolog 有它):

$ swiplWelcome to SWI-Prolog (Multi-threaded, 64 bits, Version 7.3.2-25-gf8c39d8)Copyright (c) 1990-2015 University of Amsterdam, VU AmsterdamSWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,and you are welcome to redistribute it under certain conditions.Please visit http://www.swi-prolog.org for details.For help, use ?- help(Topic). or ?- apropos(Word).?- findall(X, between(1,3,X), Xs, Rest), findall(Y, between(7,11,Y), Rest).Xs = [1, 2, 3, 7, 8, 9, 10, 11],Rest = [7, 8, 9, 10, 11].

几乎所有需要展平列表的情况都可以使用差异列表来避免。

关于list - 展平 Prolog 中的列表列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30880021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com