Fibonacci numbers in Factor
Example for versions
Factor 0.94
This example shows recursive calculation of Fibonacci numbers.
Word fib
calculates the n-th number: if the argument is not greater than 1, it stays on the stack as the return value, otherwise it is replaced with a sum of previous numbers. Word bi
is a short-hand version of cleave
combinator and allows to apply two quotations (in this case calls of fib
for smaller arguments) to the same element of the stack (n
).
USING: formatting kernel math sequences ;
IN: fibonacci-example
: fib ( n -- fib(n) )
dup
1 >
[ [ 1 - fib ] [ 2 - fib ] bi + ]
when ;
16 iota [ 1 + fib "%d, " printf ] each
"...\n" printf
Comments
]]>blog comments powered by Disqus
]]>