mers/examples/fib.mers

28 lines
598 B
Plaintext
Executable File

fib := n -> {
// we start with these two values
v := (0, 1)
{() -> {
// subtract 1 from n
&n = (n, -1).sum
// extract the latest values
(l, r) := v
// if n is still positive...
if (n.signum, 1).eq {
// ...advance the fib. sequence
&v = (r, v.sum)
// and don't break from the loop
()
} else {
// n is zero or negative (happens the n-th time this loop runs),
// so we break with the latest value
(r)
}
}}.loop
}
((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 25, 50, 100), i -> {
i.print
": ".print
i.fib.println
}).for_each