Cause I have a lazy-seq
to calculate fibonacci sequence.
(def fibonacci
(lazy-cat [0 1] (map + fibonacci (rest fibonacci))))
=> #'user/fibonacci
(take 10 fibonacci)
=> (0 1 1 2 3 5 8 13 21 34)
But when I try to put fibonacci
into let
(let [fibonacci
(lazy-cat [0 1] (map + fibonacci (rest fibonacci)))]
(take 10 fibonacci))
CompilerException java.lang.RuntimeException: Unable to resolve symbol: fibonacci in this context, compiling:...
How to solve it?
Unlike with def
, the binding of x
to f
made by (let [x f] ...)
is not visible inside f
. More precisely, binding to x
is done after evaluating f
. In order to have recursive definitions, you need to use letfn
, which is for defining functions. Consequently, you can no longer treat fibonacci
as a LazySeq
, but you can define it as a function returning a LazySeq
:
(letfn [(fibonacci []
(lazy-cat [0 1] (map + (fibonacci) (rest (fibonacci)))))]
(take 10 (fibonacci)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With