Archive

Posts Tagged ‘swi prolog’

Example Code #3 – Basics in Prolog

December 28th, 2008 No comments

1- Parent relation between rex, doggie and goldie

dog(rex).
dog(X):-parent(X,Y).
parent(goldie,rex).
parent(jack,rex).

Query -> ?dog(goldie) : YES
Query -> ?dog(jack) : YES

2- Sister relation

sister(X,Y):-girl(X),girl(Y),parent(X,Z),parent(Y,Z).
parent(sara,Maria).
parent(lili,Maria).
girl(lili).
girl(maria).

Query -> ?sister(lili,maria) : NO

3- Finds factorial of numbers

1!: 1 , 2!: 2×1, 3!: 3x2x1
Here you can see that we are able to say 3!: 3×2! instead of 3!:3x2x1 . Since i know what is 1! and 0! therefore i have limitation points for my program.

fact(0,1):-!.
fact(1,1):-!.
fact(N,F,F1) :- N1 is N-1, fact(N1,F1), F is N * F1.

Read more…

VN:F [1.9.11_1134]
Rating: 1.0/10 (1 vote cast)
VN:F [1.9.11_1134]
Rating: 0 (from 0 votes)