且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从列表列表中获取元素

更新时间:2022-05-27 22:05:57

在SWI-Prolog(可能还有其他)中,您可以使用flatten/2:

In SWI-Prolog (and maybe others), you can use flatten/2:

?- flatten([[[a,b,[c]],d,e],f,g,[h,[i,j]]], S).
S = [a, b, c, d, e, f, g, h, i|...].

请注意, flatten/2的SWI-Prolog手册页包含以下语句:

Note that the SWI-Prolog manual page for flatten/2 includes the following statement:

最终需要用flatten/3表示(例如,append/3用于附加两个列表),这是一个不好的设计.

Ending up needing flatten/3 often indicates, like append/3 for appending two lists, a bad design.

但是,该页面没有说明是否还有另一个本机谓词来代替它.

However, the page doesn't say whether there is another native predicate to replace it.

我相信将会提供更好的答案.

I'm sure a better answer will be supplied.