I am new to prolog and i want to solve this problem. Suppose I have a list say
List i.e. [a,b,c]
now i have some facts say
likes(a,banana).
likes(b,orange).
likes(c,apple).
likes(d,grapes).
So if I make a query
?- my_functor(List,X).
X=[banana,orange,apple].
Thanks you.
Consider:
Explanation:
findall/3is called an ‘all-solutions’ predicate which seeks to find all possible values unifiable to the first argument (here, that’s the variableX) to solutions for the seconds argument (here, that’s the conjunction(member(Y, List), likes(Y, X))), and places all values forXinto a list, bound to the third argument (here, that’sXs).Notice that the inner expression generating the values for
Xis a statement that backtracks to provide different assignments forX:Tested with SWI-Prolog.
Note that
findall/3also appears in GNU Prolog amongst most other implementations.