% code_2009_04_15.pro % Glenn G. Chappell % 15 Apr 2009 % % For CS 331 Spring 2009 % Sample Prolog code % From the Wednesday, April 15, 2009 class meeting % father(A, B) means A is the father of B. father(glenn, nathaniel). father(glenn, theresa). % mother(A, B) means A is the mother of B. mother(joanne, nathaniel). mother(joanne, theresa). mother(mary, joanne). % grandmother(A, B) means A is the grandmother of B. grandmother(A, B) :- mother(A, X), mother(X, B). grandmother(A, B) :- mother(A, X), father(X, B). % fact(A, B) means A! = B. % fact(A, B) can find B, given A. fact(0, 1). fact(A, B) :- A > 0, X is A-1, fact(X, Y), B is A*Y. % fibo(A, B) means F(A) = B, that is, the Ath Fibonacci number is B. % fibo(A, B) can find B, given A. fibo(0, 0). fibo(1, 1). fibo(A, B) :- A > 1, S is A-1, fibo(S, Sf), T is A-2, fibo(T, Tf), B is Sf + Tf. % len(X, A) means X is a list, and its length is A. % len(X, A) can find A, given X. len([], 0). len([_|T], A) :- len(T, B), A is B+1.