I'm quite new in Prolog. I'm trying to find the nth term and sum of a Fibonacci Series.
/* Fibonacci */
predicates
fibonacci(integer, integer, integer)
clauses
fibonacci(1,1,1):-!.
fibonacci(2,1,2):-!.
fibonacci(N, Term, Sum):-
N1 = N - 1,
N2 = N - 2,
fibonacci(N1, Term1, Sum1),
fibonacci(N2, Term2, Sum2),
Term = Term1 + Term2,
Sum = Term + Sum.
However while compiling in Turbo Prolog I'm getting 420 PROLOG.ERR missing on
fibonacci(N2, Term2, Sum2),
Why is this happening? Any help is appreciated. Thanks in advance.
Is that really the entire error message? It does not say what is missing?
In any case: Assuming that Turbo Prolog's
clauses
part corresponds to standard Prolog, none ofN1
,N2
,Term
andSum
will be integers in your program.=
means unification, not arithmetic evaluation. If you callfibonacci(3, Term, Sum)
, then inside the callN1
will be bound to the uninterpreted term3 - 1
, not to the integer 2. The same goes for your other uses of=
.For the arithmetic part, you will want to use
is/2
:N1 is N - 1
,N2 is N - 2
etc. This will evaluate the right-hand side as an arithmetic expression and actually bind these variables to integers.Without thinking about it too hard, it's not clear to me if this will result in a useful computation for
Term
.