从列表列表中获取元素

从列表列表中获取元素

问题描述:

是否可以从Prolog中的列表列表中获取所有元素?

is it possible to get all elements from list of lists in Prolog?

类似的东西:我们有getElements([[[[a,b,[c]],d,e],f,g,[h,[i,j]]],S),结果是:S = [a,b,c,d,e,f,g,h,i,j] ...

Something like: We have getElements([[[a,b,[c]],d,e],f,g,[h,[i,j]]],S) and the result is: S = [a,b,c,d,e,f,g,h,i,j] ...

感谢您的帮助.

在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.