To add one small addendum to Mark's excellent comment, if you use lazy-
seq then you don't need to worry about the nil from when

On Jul 24, 12:01 pm, Mark Engelberg <mark.engelb...@gmail.com> wrote:
> On Sat, Jul 24, 2010 at 11:45 AM, Mark Engelberg
>
> <mark.engelb...@gmail.com> wrote:
> > The simplest translation is to wrap a lazy-seq around the last line to
> > avoid the stack overflows.
>
> Just to clarify, there are at least three reasonable places to place
> the call to lazy-seq.  You can put lazy-seq around the full body of
> the function.  You can place it around the cons.  You can place it
> around the recursive call to scheme-remove-first.  Each choice results
> in slightly different laziness behavior, i.e., when various elements
> are computed, but the overall semantics of the sequence remains the
> same and stack overflows will be avoided.  Placing the lazy-seq around
> the recursive function call will cause scheme-remove-first to compute
> the first element right away, and delay the rest.  Placing the
> lazy-seq around the full body will prevent any computation until it is
> asked for by a consumer.  Placing the lazy-seq around the cons results
> in in immediate behavior for the nil and
> removable-item-at-front-of-list case, and delayed behavior otherwise.
> All are acceptable choices, but preferences vary.  Probably placing
> lazy-seq around the full body is the most common style you'll see in
> Clojure, although I tend to place it where the laziness is actually
> required (like around the recursive call, or around the cons).
>
> You've probably noticed from the other samples posted that many
> Clojurians prefer to use (seq lst) instead of (not (empty? lst)), and
> organize their code around the not-empty case.
>
> So (if (empty? lst) empty-case not-empty-case) becomes (if (seq lst)
> not-empty-case empty-case)
>
> When the empty case also results in nil, you can replace the if
> structure with a one-armed when, because when automatically returns
> nil in the other case.
>
> So (if (seq lst) not-empty-case nil) becomes (when (seq lst) not-empty-case).

-- 
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
Note that posts from new members are moderated - please be patient with your 
first post.
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