Samet Kilictas’s Blog

Example Code #3 – Basics in Prolog

by Samet Kilictas on Dec.28, 2008, under Programming, Prolog

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!: 3×2x1
Here you can see that we are able to say 3!: 3×2! instead of 3!:3×2x1 . 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.

4- Finds Fibonacci numbers

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,…

fib(0,0):-!.
fib(1,1):-!.
fib(X,L) :- X > 1, Y1 is X-1, Y2 is X-2, fib(Y1,F1), fib(Y2,F2), L is F1+F2.

VN:F [1.0.6_327]
Rating: 0.0/10 (0 votes cast)
:, , , , ,
No comments for this entry yet...

Leave a Reply

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...