On Thursday, 17 April 2014 04:28:02 UTC+8, John Mastro wrote:
>
> > I assume that there's a good reason that iterate returns a Cons 
> > instead of a LazySeq.
>
> IIUC, this particular case arises because iterate's body is implemented
> as
>
>     (cons x (lazy-seq (iterate f (f x))))
>
> rather than
>
>     (lazy-seq (cons x (iterate f (f x))))
>
> Can anyone comment on whether there's a reason to prefer one over the
> other?
>

The difference is that the former is slightly less lazy - the Cons cell 
containing x is constructed immediately rather than being deferred as part 
of the lazy seq.

This probably performs slightly better in some circumstances, and since you 
already have x as a value it probably makes sense to do this eagerly since 
no arbitrary computation is being done (f doesn't need to be called yet in 
either 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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to