Fibonacci numbers
*:'(+\|:)\[20;1 1]
This (in my eyes beautiful) program calculates the first 20 fibonacci numbers.
It's actually rather simple. The +\|:
operator is composed out of reversion (|:
) followed by "traced" addition (+\
):
(a,b) -> (b,a) -> (b,a+b)
|: +\
Now we apply this operator 20 times to 1 1
, meanwhile tracing it: (\[20;1 1]
)
1 1 -> 1 1 -> 1 2
1 2 -> 2 1 -> 2 3
2 3 -> 3 2 -> 3 5
...
The first column is returned as a list.
Now we just need to remove anything but the first element in each "sublist", that's exactly what *:'
does.