Quantcast
Channel: Samet Kilictas's Blog » Prolog
Viewing all articles
Browse latest Browse all 2

Example Code #4 – Intersect and Write in Prolog

$
0
0

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]


Viewing all articles
Browse latest Browse all 2

Trending Articles