go :- match(Mn,Fn),
write('--Matching Result--'),
nl,
write(Mn),
write(' match with '),
write(Fn),
match(Mn1,Fn1).
person(may,female,25,blue).
person(rose,female,20,blue).
person(hock,male,30,blue).
person(ali,male,24,blue).
match(Mn,Fn):-person(Fn,'female',Fage,Fatt),
person(Mn,'male',Mage,Matt),
Mage>=Fage,
Fatt=Matt.
Hi,this is my code...but it's only can show the 1 output...but there are 3 pair of matching in match(X,Y).how to show them all in my go function.
Thank you
You get all your matches if you force backtracking, usually by entering
;
(e.g. in SWI Prolog). But you also see that you are getting unnecessary outputstrue
. This is because the last clause ingo
ismatch(Mn1,Fn1)
. This clause succeeds three times and binds the variablesMn1,Fn1
but then onlytrue
is output, because you do notwrite()
after that clause. The fourth timematch(Mn1,Fn1)
fails and by backtracking you come back to the first clausematch(Mn,Fn)
that matches, the match is output, etc.You surely do not want to have this behavior. You should remove the last clause
match(Mn1,Fn1)
ingo
. Now by pressing;
you get the 3 matches without any outputtrue
in between.But what you likely want is that the program does the backtracking. To achieve this, you just need to force backtracking by adding
false
as the last clause. To get proper formatting of the output, use the following program. The last clausego2.
is added to gettrue
at the very end.This technique is called failure driven loop.
If you have any predicate that has multiple results and want to to find all of them, you should use
findall/3
For example, in your case, you could do something like:
L will be a list that will contain all the X,Y that satisfy match(X,Y) in the format [X,Y]. for example, assuming that:
the result will be L = [ [m1,f1], [m2,f2] ]
note that you can define the format as you wish, for example you could write:
then you have to recurse on the list to print them.
However, if you wish to find one result, run some code and then continue you could use forall/2:
Prolog is a lazy language. Which means that it will stop once it has found a condition that made your problem true. This will be the very first match alone.
IF your code is working (I haven't tried it), then you should try and run the
match
-statement like this in your prolog inspector:match(X,Y)
The prolog inspector will return all states and print them for you.