0

I'm struggling to understand why fib([1,2],F) finds a second solution, false, with the following clauses and rules:

fib([A,B|C],F) :-
   fib([B|C],S),
   fib(C,T),
   F is (S+T).
fib([A],1).
fib([],0).

According to my logical analysis of fib([1,2],F), I should just be getting the result F = 2. I need some help to understand why Prolog finds a second solution, which is false.

false
  • 10,264
  • 13
  • 101
  • 209
Måns Nilsson
  • 431
  • 1
  • 4
  • 16
  • What exactly do you want fib/2 predicate to do ?? – coder Oct 01 '16 at 07:09
  • I don't think there is a second answer. If you're getting the first answer and then manually asking prolog to give you a second one, it responds with `false` to say there are no other solutions. – Enigmativity Oct 01 '16 at 09:36

1 Answers1

3

Sorry but I don't understand what your logical analysys can be.

I can say that, from your code, with fib([1,2], F) I get only F = 1, not F = 2; and it's because fib([1,2], F) become

fib([1,2],1) :-
  fib([1],1),
  fib([],0),
  1 is (0+1).

false isn't a second solution but (if I'm not wrong) a failed attempt to obtainig (via backtracking) a second solution.

max66
  • 65,235
  • 10
  • 71
  • 111
  • 2
    That's exactly right. When the toplevel says `false`, it means that there are no further solutions. – mat Oct 01 '16 at 08:28