0

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.

false
  • 10,264
  • 13
  • 101
  • 209
lu5er
  • 3,229
  • 2
  • 29
  • 50
  • 1
    `fibonacci(N2, Term2, Sum2)` looks like `Sum2` isn't used anywhere else. Is that intended? Although I don't know why that would be flagged as an error. It should be a warning. You should also rethink your implementation. Two recursive calls is very inefficient and unneeded. If you search for "fibonacci" in this section of Stackoverflow, you'll find lots of examples. – lurker Jan 05 '17 at 17:45
  • ok.. searching on SO for better solution. But just interested to know what is the error? :) – lu5er Jan 05 '17 at 17:54
  • @lurker I guess you figured it out right! Since I'm not using that term TP is throwing this. I just used a dummy variable to store the value like `dummy = Sum2`. Now the error shifted to this line.. – lu5er Jan 05 '17 at 18:06
  • I **strongly** recommend you try out SWI-Prolog instead of Turbo Prolog. It's free, it's from this century, it's probably used 100x as much and it has great modern libraries. – Daniel Lyons Jan 14 '17 at 13:44

3 Answers3

2

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 to is/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 for Sum2.

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.

Isabelle Newbie
  • 9,258
  • 1
  • 20
  • 32
  • I think that the part you pointed out is incorrect. Refer to `lyra.ifas.ufl.edu/ABE6644/prologexamples.txt`. Turbo Prolog allows `=` as assignment. So, I don't think that is the problem. – lu5er Jan 05 '17 at 17:26
  • 1
    @Isabelle, unfortunately, Turbo Prolog (or Visual Prolog, or PDC Prolog, mostly the same) isn't very standard. In TP, they use declared sections of code, like `clauses` and `predicates`. Also, in TP, `=` acts like ISO `is/2` – lurker Jan 05 '17 at 17:37
  • @Apy I do agree with Isabelle here that your quoted error message seems incomplete. "Missing" what? – lurker Jan 05 '17 at 17:40
  • @lurker I also don't know! It's just that `420 PROLOG.ERR missing` ! Can a screenshot help? – lu5er Jan 05 '17 at 17:52
2

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&section=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).

% 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

--- (barely tested on http://swish.swi-prolog.org/)
1

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

Kau Moo
  • 51
  • 5