Archive

Archive for January, 2009

Happy New Year

January 1st, 2009 1 comment

I was coding since an hour before the new year. I know it does not sounds good for this night however i wish the best for everyone..

Hope it will be better in 2009.

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)
Categories: General Tags: ,

Example Code #4 – Intersect and Write in Prolog

January 1st, 2009 1 comment

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.9.22_1171]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)