On Aug 31, 1:58 pm, Achim Passen <[EMAIL PROTECTED]> wrote:
> 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.
This is not self-referential, but mutually referential - fibs2 is a
sequence, the result of a call to an anonymous function which refers
to that sequence via fibs2.
There is no local mutual reference (i.e. letrec) in Clojure at
present, short of the Y combinator:
http://groups.google.com/group/clojure/browse_frm/thread/3259f09e08be4cfd/21f0e19fdce15ae9
>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?
>
The evaluation is delayed, but the name resolution isn't.
Rich
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---