Fibonacci - reverse output list

Hello. I am writing a predicate to calculate the terms of the fibonnacci suite. I managed to write fibo (X, Y) where Y is the term number X of the sequel. But my goal was to flip the list of terms from 0 to X. If anyone can help me. Thank you in advance.

fibo (N, N): - No.2,!. 
fibo (N, R): -
     N1 is N-1, N2 is N-2,
     fibo (N1, R1), fibo (N2, R2),
     R is R1 - R2.

I tried to use you code, but don’t understand the intent of

No.2,

Here are examples of working Fibonacci in Prolog

To reverse a list in Prolog see reverse/2

Sorry. It was a mystake.

fibo(N, N) :- N<2, !.
fibo(N, R) :-
N1 is N-1, N2 is N-2,
fibo(N1, R1),fibo(N2, R2),
R is R1 + R2.

Upon first reading without running your code I thought you might be writing the values of the Fibonacci as a list but upon running your code

?- fibo(5,R).
R = 5.

?- fibo(10,R).
R = 55.

I see that it is only returning a single value.

So, I don’t know what But my goal was to flip the list of terms from 0 to X. means.

The exercice want me to flip the list of terms from 0 to X. But I dont know how to make it. when I run the code step by step, it calculates all the terms up to the X term. I’d like to add in a list each calculated term. So I’ll have the list of terms from 0 to X. But I don’t know how to do that.

Can you please post the exercise instructions as given, it would be appreciated.

The way I typically give a predicate the ability to return a list is to add the list value as another argument to the predicate and then modify the predicates accordingly. e.g. start with

fibo(N, N, List)

and then

fibo_2(N, N, []) :- N<2, !.
fibo_2(N, R, [R1,R2|T2]) :-
	N1 is N-1, N2 is N-2,
    fibo_2(N1, R1, _),fibo_2(N2, R2, T2),
     R is R1 + R2.

however when I did that it didn’t work as I expected,

?- fibo_2(6,R,L).
R = 8,
L = [5, 3, 2, 1, 1, 0].

but it was very close and shows how to add a list to what you currently have.

So to understand what was happening I modified the code to print the values along the way.

fibo_2(N, N) :- N<2, !.
fibo_2(N, R) :-
	N1 is N-1, N2 is N-2,
    fibo_2(N1, R1),fibo_2(N2, R2),
     R is R1 + R2,
     format('~nN: ~w, R1: ~w, R2: ~w',[N,R1,R2]).

and ran an example.

?- fibo_2(6,R).

N: 2, R1: 1, R2: 0
N: 3, R1: 1, R2: 1
N: 2, R1: 1, R2: 0
N: 4, R1: 2, R2: 1
N: 2, R1: 1, R2: 0
N: 3, R1: 1, R2: 1
N: 5, R1: 3, R2: 2
N: 2, R1: 1, R2: 0
N: 3, R1: 1, R2: 1
N: 2, R1: 1, R2: 0
N: 4, R1: 2, R2: 1
N: 6, R1: 5, R2: 3
R = 8.

While I haven’t completed this for your exercise, it should give you enough so that you can solve the problem on your own. :smiley:

If you try fibo_2(7, R, L). you will have L = [8, 5, 3, 2, 1, 1], and R = 8.
it returns the term number 6. This problem seems easy to solve. But the second problem is that 0 did not among the terms. And it is always the case for fibo_2 (N, R, L) where N is odd.

Hello. Thanks for your help. I have found a solution.

fibo(N, N, [N, 0]) :- N is 1, !.
fibo(N, N, [0]) :- N is 0, !.
fibo(N, R, [R, R1|T2]) :-
    N1 is N-1, N2 is N-2,
    fibo(N1, R1, _),fibo(N2, R2, T2),
    R is R1 + R2.
1 Like