/* fib1.pl:  Straight-forward implementation of a predicate that
 * recursively (and thus inefficiently!) computes a specific term
 * of the Fibonacci sequence.  We'll assume that f(n)=n when n = 0 or 1.
 *
 * Form:  fib1(S,V), where S is the sequence number and V is the Fibonacci
 * sequence value at position S.
 */

fib(0,0).  % The 0th Fibonacci number is 0.
fib(1,1).  % The 1st Fibonacci number is 1.

fib(Seqnum,Value) :- Seqnum > 1,             % if not, already handled by facts
                     S1 is Seqnum-1,         % compute preceding two seq #s
                     S2 is Seqnum-2, 
                     fib(S1,V1), fib(S2,V2), % compute those fib values
                     Value is V1+V2.         % and sum to produce the result
