On Fri, Dec 12, 2008 at 6:51 PM, levand <luke.vanderh...@gmail.com> wrote:
> The next implementation is simpler, and uses recursion quite
> elegantly:

> (defn copy-seq [source]
>    (if (nil? source)
>      '()
>      (cons (first source) (copy-seq (rest source)))))

> What would one do in a language like Scheme, without lazy-
> cons?

In PLT Scheme, you'd write it just like your elegant recursive
version.  This is such a common pattern that the runtime is optimized
in such a way that there's no real stack limitations and you never
have to worry about blowing the stack (unless the actual result list
is too big for your computer's memory, of course).

In environments with a limited call stack, you'd do the accumulator
version and reverse it at the end.  In implementations that support
mutable lists, it is not uncommon to reverse the copy destructively to
save a bit of space.

I'm a relative newbie to Clojure, but I would think that in Clojure,
using lazy-cons would probably be the preferred approach, although the
accumulator version followed by an accumulator version of reverse is
not unreasonable if you have some concerns about being lazy.

--Mark

--~--~---------~--~----~------------~-------~--~----~
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 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to