mers/examples/fib.mers

28 lines
590 B
Plaintext
Raw Normal View History

2023-10-19 18:46:15 +02:00
fib := n -> {
// we start with these two values
v := (0, 1)
loop {
2023-10-19 18:46:15 +02:00
// 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),
2023-10-28 12:53:12 +02:00
// so we break with the latest value
2023-10-19 18:46:15 +02:00
(r)
}
}
2023-10-19 18:46:15 +02:00
}
((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 25, 50, 100), i -> {
i.print
": ".print
i.fib.println
}).for_each