从列表列表中获取元素 [英] Get elements from list of lists

查看:79
本文介绍了从列表列表中获取元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从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.

这篇关于从列表列表中获取元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆