While learning gforth
I have implemented the Fibonacci sequence in a few forms.
Below are some implementations of the fibsum definition, which sums up all the Fibonacci values up to a certain rank.
In other words the Fibonacci sequence being: 0 1 1 2 3 5 8 13 21 34 55 89
fibsum will produce: 0 1 2 4 7 12 20 33 54 88 ....
Here is my first implementation of fibsum
variable fbsum
: fibsum ( u -- u.sum ) { p } 0 1 \ Fibonacci-Sum.
\ This sums the first u values of the Fibonacci sequence.
0 fbsum !
p 0= if drop dup else 1 fbsum ! endif
p 1 u+do
over over +
dup fbsum +!
rot drop
loop
2drop fbsum ? ;
Here is the second implementation
variable fbsum
: fibsum2x ( u -- u.sum ) { p } 0 1 \ Fibonacci recursive (1)
p dup 1 > if
dup 1- recurse swap
2 - recurse +
nip nip
else
p 1 = if 1 fbsum +! endif \ Here p matches the argument to the top call!
nip nip
endif ;
: fibsum2 ( u -- u.sum ) \ Fibonacci-Sum.
\ This sums the first u values of the Fibonacci sequence.
{ m }
0 fbsum !
m 1+ 1 u+do
i fibsum2x drop
loop
fbsum ? ;
And this is the third implementation
variable fbsum
: fibsum3x ( u -- u.sum ) { p } 0 1 \ Fibonacci recursive (2)
p dup 1 > if
dup 1- recurse swap
2 - recurse +
nip nip
else
\ p 1 = if 1 fbsum +! endif \ Here p matches the argument to the top call!
nip nip
endif ;
: fibsum3 ( u -- u.sum ) \ Fibonacci-Sum.
\ This sums the first u values of the Fibonacci sequence.
{ m }
0 fbsum !
m 1+ 1 u+do
i fibsum3x
fbsum +!
loop
fbsum ? ;
As one can see I am not making use of the return stack in my code. In fact I tried but failed. So here comes my question.
How can I modify the code above and make a correct use of the return stack ?
A few notes.
Gforth is an implementation for the Forth programming language. Implementations for Forth are also known as Forth systems. It's better to learn Forth in general (using Gforth, or another Forth system) than the Gforth system particularities. For example, use
{: :}
to declare local variables, instead of{ }
.Usually, if you use variables, whether they are static or local (temporary), you have less need for the return stack to rearrange arguments. Therefore, to demonstrate the use of the return stack, we will eliminate variables.
The return stack is just an additional stack available with known limitations for a Forth program. The return stack cannot be used to pass arguments into a definition, or return them from a definition.
To correctly pose the problem on the Fibonacci sequence, we should describe it more carefully to show what indexing is used. From the recurrence relation:
it's obvious that we start from
0
.Just for reference, the word
fib
can be inefficiently implemented from this recurrence relation like this:So, the sum of the Fibonacci sequence elements up to a certain rank means the sum of elements up to the certain index, starting from
0
. And the recurrence relation forfibsum()
is following:Let's define the test cases for the word
fibsum
from the very beginning:In the Gforth distribution, the words
t{ -> }t
are defined in test/ttester.fs, include this file to run the above tests.From the recurrence relation for
fibsum()
we can get the following recursive implementation for the wordfibsum
:And an iterative implementation is even shorter (but slightly more complicated):
These implementations are simple, but they perform a lot of excessive computation.
An efficient way is to calculate both the value and the sum on each step. The idea is that we define the word
fibsum-next ( u1 u2 u.sum2 -- u2 u3 u.sum3 )
, that calculates the next value and next sum from the previous value and previous sum; actually, to calculate the next value, we should use two previous values:u3=u1+u2
,u.sum3=u3+u.sum2
.The test cases:
NB: this word returns the correct values recurrently starting from the index 2 only, i.e., staring from the input
( 0 1 1 )
.An implementation without using the return stack:
And the variant with using the return stack (finally!):
In this case the use of the return stack does not make code shorter, but can be easier to implement, and more efficient.
As a beginner, you'll probably want to write more stack diagrams, like this:
In the implementation for
fibsum
we should correctly prepare the initial arguments forfibsum-next
and run the loop:In this case, it's difficult to rearrange the arguments without an additional stack or variables. And we just used the return stack.
Well, we can use the word
roll
, but it's usually very inefficient.