# Fibonacci - reverse output list

**URL:** <https://swi-prolog.discourse.group/t/fibonacci-reverse-output-list/1651>\
**Category:** Help!\
**Created:** [December 22, 2019, 2:54pm UTC](https://swi-prolog.discourse.group/t/fibonacci-reverse-output-list/1651 "2019-12-22T14:54:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![AdNana](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@AdNana](https://swi-prolog.discourse.group/u/AdNana)\
**Post date:** [December 22, 2019, 2:54pm UTC](https://swi-prolog.discourse.group/t/fibonacci-reverse-output-list/1651/1 "2019-12-22T14:54:23Z")

</div>

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.

```prolog
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.

```

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [December 22, 2019, 3:16pm UTC](https://swi-prolog.discourse.group/t/fibonacci-reverse-output-list/1651/2 "2019-12-22T15:16:33Z")

</div>

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

```prolog
No.2,

```

Here are examples of working Fibonacci in [Prolog](https://rosettacode.org/wiki/Fibonacci_sequence#Prolog)

To reverse a list in Prolog see [reverse/2](https://www.swi-prolog.org/pldoc/man?predicate=reverse/2)

---

<div class="post-metadata">

**Author:** ![AdNana](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@AdNana](https://swi-prolog.discourse.group/u/AdNana)\
**Post date:** [December 22, 2019, 3:37pm UTC](https://swi-prolog.discourse.group/t/fibonacci-reverse-output-list/1651/3 "2019-12-22T15:37:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [December 22, 2019, 3:45pm UTC](https://swi-prolog.discourse.group/t/fibonacci-reverse-output-list/1651/4 "2019-12-22T15:45:58Z")

</div>

> [@AdNana](#):
>
> But my goal was to flip the list of terms from 0 to X.

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

```prolog
?- 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.

---

<div class="post-metadata">

**Author:** ![AdNana](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@AdNana](https://swi-prolog.discourse.group/u/AdNana)\
**Post date:** [December 22, 2019, 3:52pm UTC](https://swi-prolog.discourse.group/t/fibonacci-reverse-output-list/1651/5 "2019-12-22T15:52:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [December 22, 2019, 4:25pm UTC](https://swi-prolog.discourse.group/t/fibonacci-reverse-output-list/1651/6 "2019-12-22T16:25:46Z")

</div>

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

> [@AdNana](#):
>
> I’d like to add in a list each calculated term.

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

```prolog
fibo(N, N, List)

```

and then

```prolog
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,

```prolog
?- 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.

```prolog
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.

```prolog
?- 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. 😃

---

<div class="post-metadata">

**Author:** ![AdNana](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@AdNana](https://swi-prolog.discourse.group/u/AdNana)\
**Post date:** [December 22, 2019, 5:28pm UTC](https://swi-prolog.discourse.group/t/fibonacci-reverse-output-list/1651/7 "2019-12-22T17:28:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![AdNana](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@AdNana](https://swi-prolog.discourse.group/u/AdNana)\
**Post date:** [December 23, 2019, 11:54am UTC](https://swi-prolog.discourse.group/t/fibonacci-reverse-output-list/1651/8 "2019-12-23T11:54:08Z")

</div>

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

```prolog
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.

```
