prologprolog-setof

run function with all possibilities resulted from other function


I have two predicates:

foo(Y,X)
bar(Y,Z)

After running foo, How can I run bar with all possibilities of Y ?

example:

foo(Y, key) % all possibilities of Y => chat 
            %                           faq 
            %                           about
            %                           search

How can I run bar with these all possibilities ?

bar(chat, Z)
bar(faq, Z)
bar(about, Z)
bar(serach, Z)

And then store all the results of Z in a list Zs?


Solution

  • allZs(X, Zs) :-
        setof(Y, foo(Y, X), Ys),
        maplist(bar, Ys, Zs).
    

    related SWI-Prolog man pages: Finding all Solutions to a Goal and library apply

    Note: usually in Prolog the convention is to put intput arguments before output ones - in your first predicate that'd mean foo(X, Y) instead of foo(Y, X). Plus here it'd outline the transitivity: foo(X, Y), bar(Y, Z)..