[REBOL] Re: Fun with literal blocks!
From: joel:neely:fedex at: 27-Oct-2000 7:30
[rebol-bounce--rebol--com] wrote:
> How about some non-curmudgeonly comments on a unique REBOL idiom, the
> appearance of literal blocks in function bodies?
>
It is possible to make the memo-izing Fibonacci function a bit more
tense, but at the usual cost of decreased obviousness. The prior
version is called Fibon below, the slightly-more-bummed version is
the new Fibonacci:
fibon: func [n [integer!] /local t cache] [
cache: [1.0]
either n < 0 [
t: fibon - n
either odd? n [ t ][ - t ]
][
either n = 0 [
0.0
][
while [n > length? cache] [append cache none]
if none? t: pick cache n [
poke cache n
t: (fibon (n - 1)) + (fibon (n - 2))
]
t
]
]
]
The reason for using Pick and Poke would be to make explicit the
relationship between N and T; the purpose of the None-appending
loop was to ensure that Poke would not be grumpy.
We can leave N implicit upon caching and arrive at:
fibonacci: func [n [integer!] /local t cache] [
cache: [1.0]
either n < 0 [
t: fibonacci - n
either odd? n [ t ][ - t ]
][
either n = 0 [
0.0
][
if none? t: cache/:n [
append cache
t: (fibonacci (n - 1)) + (fibonacci (n - 2))
]
t
]
]
]
-jn-