This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/CLPQR/clpqr/examples/monash/fib

35 lines
802 B
Plaintext
Raw Normal View History

/*
**********************************************************************
*
* CLP(R) Version 2.0 (Example Programs Release)
* (C) Copyright, March 1986, Monash University
*
**********************************************************************
*/
%
% Simple recursive formulation of fibonacci numbers.
% The goal ?- go shows how the definition can be used for
% both finding a a fibonacci number given its index, and finding the index
% of a given fibonacci number.
%
fib(0, 1).
fib(1, 1).
fib(N, X1 + X2) :-
N > 1,
fib(N - 1, X1),
fib(N - 2, X2).
go:- fib(10, Z),
printf("forward: fib(10) = %d\n",[Z]),
fib(Y, Z),
printf("backward: fib(%d) = %d\n",[Y,Z]).
% Answer:
% forward: fib(10) = 89
% backward: fib(10) = 89
?- printf("\n>>> Sample goal: go/0\n", []).