Search code examples
listprologpredicate

How to create a list compose from names of a predicate in prolog


How to create a list compose from names of a predicate example: name(a), name(b), name(c), name(d)

I want to achieve a list like L=[a,b,c,d].

Please help me someone with code or some advice.


Solution

  • findall/3 builds a list for you, if the predicate name and arity is known just use it to get the list L

    ?- findall(X,name(X),L).
    

    If the predicate name is unknown, you can use call/N where N is the arity of the predicate plus one. For instance:

    ?- [user].
    |: a_name(a).
    |: a_name(b).
    |: a_name(c).
    |: ^D% user://1 compiled 0.01 sec, 3 clauses
    true.
    
    ?- Name =a_name,findall(X,call(Name,X),L).
    Name = a_name,
    L = [a, b, c].