views:

76

answers:

2

Hi everyone,

I'm new to prolog and I just can't figure it out.

I'm trying to build a simple program that receives a list of predicates, searchs for a specific predicate in the list, and applies a function to that predicate parameters.

something along these lines:

?- program([pred1(a,b,p), pred2(d,b,p), pred2 (a,c,p)]). 

program (list1) :- 
search(pred2(X,Y,p),list1).
doSomething (X,Y) % with the X and Y returned from search function, both of them.

Basically, I want to use all values that would return from an objective search(pred2(X,Y,p),list1) and use them on another function.

Thanks in advance

+2  A: 

For starters, I would avoiding calling these things "functions". Prolog is not a procedural language, and rules / predicates are not functions.

Basically, when you use a rule you're really asking Prolog, "give me all the values which will satisfy this rule". The rule, by itself, does not return anything.

So say you had the following in a procedural language:

f(g(3))

How would you do it in Prolog? You would need to write some predicate f(X,Y) and some predicate g(X,Y), and then you would need to use the query f(3,Y), g(Y,Z) - which means to ask Prolog to find you values for Y and Z which will satisfy this. Z is what you're interested in.

Oak
A: 

okay, I tried some stuff on prolog and came to this:

member(X, [X | _]).

member(X, [_ | R]) :- member(X, R).

prog(L1,Out) :- member(pred2(X,Y), L1).

?- prog ([(pred1(a,b),pred2(c,b),pred2(d,a)],Out).

It gives true 2 times as supposed but I wanted to get Out = [c,b] and Out = [d,a]. Don't know how I can achieve this, if someone could explain I would appreciate.

Reguarding Oak's answer I get that it isnt a procedural language but cant figure out how to access values and use them in prolog. Your example wasn't that helpfull.

d0pe
The right rule `prog(L1, Out):- member(Out, L1).` Then if you'll ask `?- prog([(pred1(a,b),pred2(c,b),pred2(d,a)], pred(X,Y)).` it will give you `(X=c, Y=b); (X=d, Y=a)`. But if you insist on `Out = [c,b]` than `prog(L1, [X,Y]) :- member(pred2(X,Y), L1).`
ony
To clarify ony's great comment: when you write `prog(L1,Out) :- member(pred2(X,Y), L1)` you're actually saying "I don't care what the value of `Out` is" - because `Out` doesn't appear anywhere in the rule body.
Oak