6 ms·
You can also recursively define fib as a lazy sequence that is the pairwise sum of fib and fib shifted by one. (Clojure, from Rosetta Code). (def fib (lazy
by StackOverlord 3y ago
You can also recursively define fib as a lazy sequence that is the pairwise sum of fib and fib shifted by one. (Clojure, from Rosetta Code).
(def fib (lazy-cat [0 1] (map + fib (rest fib))))
=> (take 10 fib)
(0 1 1 2 3 5 8 13 21 34)
Explanation:
0 1 1 2 3 5 ; this is fib
+ 1 1 2 3 5 8 ; this is (rest fib)
---------------
1 2 3 5 8 13 ; this is (map + fib (rest fib))
; and the sequence needs to be initialized with (lazy-cat [0 1] ...