Fibonacci numbers in APL
Example for versions
Dyalog APL 13.1
This example uses an anonymous D-function which calls itself recursively. The first expression of the function processes the case of the first two Fibonacci numbers, returning 1. The rest of them are processed by the second expression, which calls this very D-function (function ∇
) for smaller indices of the numbers and sums them. Overall the first line of code associates the calculated array of numbers with name fib
and outputs nothing.
The second line converts the array to required format: each element gets concatenated with a comma separator, all elements of the resulting array get concatenated with each other (,/
), and the result is padded with ...
. The overall output of this line looks exactly as required:
1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 , 144 , 233 , 377 , 610 , 987 , ...
fib←{⍵≤2:1 ⋄ (∇⍵-1)+∇⍵-2}¨1+⍳16
(⊃(,/{⍵, ', '}¨fib)),'...'
Comments
]]>blog comments powered by Disqus
]]>