[REBOL] Re: Stack depth & Recursion
From: joel:neely:fedex at: 10-Jul-2001 14:11
Hi, all,
Slight correction, to prevent my face from being *too* red...
Joel Neely wrote:
> ... and can do the canonical Fibonacci example (using a helper
> function) as follows:
>
> -fib: recurrence [
> "tail-recurrence function for Fibonacci numbers"
> a [integer!] "current value"
> b [integer!] "prior value"
> n [integer!] "Fibonacci index desired"
> ][
> n <= 0
> ][
> a
> ][
> a + b a n - 1
> ]
>
> fib: func [i [integer!]] [-fib 0 1 i]
>
Of course, there's no point in making FIB a partial function
when it's so easy to make it a total function instead!!!
.fib: recurrence [a b n] [n <= 0] [a] [a + b a n - 1]
fib: func [n [integer!]]
[
either all [negative? n even? n] [
- .fib 0 1 abs n
][
.fib 0 1 abs n
]
]
use [i]
[
repeat j 21 [i: j - 11 print [i fib
i]]
]
-10 -55
-9 34
-8 -21
-7 13
-6 -8
-5 5
-4 -3
-3 2
-2 -1
-1 1
0 0
1 1
2 1
3 2
4 3
5 5
6 8
7 13
8 21
9 34
10 55
-jn-
---------------------------------------------------------------
There are two types of science: physics and stamp collecting!
-- Sir Arthur Eddington
joel-dot-neely-at-fedex-dot-com