Search code examples
prolog

List from nested predicates


I want to know if it's possible to get a list of numbers from nested predicates in prolog.

I'll make an example, from:

?- elements(p(f(0,5,1), k(8, f(7,3), h(6)), 5), X).

I want in X this:

X = [0,5,1,8,7,3,6,5].

Thank you if you can help me =)


Solution

  • If your Prolog has append/2 and maplist/3:

    elements(N, [N]) :- number(N), !.
    elements(S, Ss) :- S=..[_|Es], maplist(elements, Es, Ts), append(Ts, Ss).