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?
EDIT: According to comments below, Turbo Prolog's
=
does indeed correspond tois/2
, so the remarks below, which are correct for Prolog, don't apply. According to comments on the original question, the terrible error message might be a singleton warning forSum2
.
In any case: Assuming that Turbo Prolog's clauses
part corresponds to standard Prolog, none of N1
, N2
, Term
and Sum
will be integers in your program. =
means unification, not arithmetic evaluation. If you call fibonacci(3, Term, Sum)
, then inside the call N1
will be bound to the uninterpreted term 3 - 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
.
i guessing turbo cant find some file with error descriptions. looks like tp incorrectly installed? correct this and you get more informative message.
look at http://rosettacode.org/mw/index.php?title=Fibonacci_sequence&action=edit§ion=399 and modify it for not only finding Nth but Sum also.
you get something like: ----
% fibsum(i, n, fib(i-2), fib(i-1), fib(i), sum(i-1), sum(i)) fibsum(N, N, Fi2, Fi1, F, Si1, S) :- F is Fi2 + Fi1, S is Si1 + F. fibsum(I, N, Fi2, Fi1, F, Si1, S) :- In is I + 1, Fn is Fi2 + Fi1, Sn is Si1 + Fn, !, fibsum(In, N, Fi1, Fn, F, Sn, S).--- (barely tested on http://swish.swi-prolog.org/ )% fibs(i, fib(i), sum(i)) fibs(1, 1, 1). fibs(2, 1, 2). fibs(C, N, S) :- C > 2, fibsum(3, C, 1, 1, N, 2, S). % Generate from 3rd on
Turbo Prolog cannot find the error messages file PROLOG.ERR. That is normally installed in Turbo Prolog installation directory.
If the file is there, chek that the application path is correctly set under Setup->Directories->Turbo Directory
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.