Hi!

Is there a way to have locally bound recursive lazy sequences in  
Clojure? I'd like to define a function that uses a lazy seq as an  
interim result, so i wouldn't want to use a top-level def.

I'll try to give a short example. You can define the sequence of  
fibonacci numbers lazily-recursively like this:

        (def fibs (lazy-cat '(0 1) (map + fibs (drop 1 fibs))))

Now suppose you want to define a function which returns the nth  
fibonacci number, but you down want "fibs" around globally. My guess  
was:

        (defn fib [n]
          (let [fibs2 (lazy-cat '(0 1) (map + fibs2 (drop 1 fibs2)))]
            (nth fibs2 n)))

But this yields "Unable to resolve symbol: fibs2 in this context", so  
let doesn't allow being self-referential the way def does. I don't  
quite understand why the second arg to lazy-cat is being evaluated in  
the first place. Shouldn't evaluation be delayed until needed?

Does anyone know how to get around this?

Thanks a lot!

Kind regards,
achim

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to