Samet Kilictas’s Blog

Tag: append

Example Code #4 – Intersect and Write in Prolog

by Samet Kilictas on Jan.01, 2009, under Programming, Prolog

1- “!” character stops the query when it is false and “;” runs as “or” so it skips the false part then moves next part

smaller(X,Y):- X
X>Y,write(Y),write(' is bigger then '),write(X),!;
write('They are equal').

Query -> ?smaller(3,5) : 3 is smaller then 5
Query -> ?smaller(8,5) : 8 is bigger then 5
Query -> ?smaller(5,5) : They are equal

2- Intersect relation

Intersect function uses an external function inside. Member function checks whether the first parameter is member of the given list or not.

Query -> member(3,[3,4,5]).

member(HM,[HM|TM]).
member(XM,[HM|TM]):- member(XM,TM).

and the intersect function gives you the result on LI variable. Try it.

intersect([ ],LI,[ ]).
intersect([HI|TI],LI,LI2):- member(HI,LI),intersect(TI,LI,LI3),LI2=[HI|LI3],!;intersect(TI,LI,LI2).

Query -> intersect([3,4],LI,[4,5]) : [4]

VN:F [1.0.6_327]
Rating: 0.0/10 (0 votes cast)
Leave a Comment :, , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...