Get elements from list of lists

后端 未结 2 1999
生来不讨喜
生来不讨喜 2020-12-02 01:13

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

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

2条回答
  •  佛祖请我去吃肉
    2020-12-02 01:48

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

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

    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.

提交回复
热议问题