lazy-seq is used, typically within functions, to create a lazy sequence. seq is used wherever you want to take a collection or a sequence, and traverse through its contents. It takes the collection or sequence and returns an object, called a seq, that you can call first or next/rest on. seq can be called on objects which are lazy, or on things which are not (like vectors, maps, sets, Java arrays, strings, and many other types of things).
The function range returns a lazy sequence, and in fact it uses lazy-seq to construct the lazy sequence that it returns. I'm not sure, but I think if you look at any function that returns a lazy sequence, it either uses lazy-seq itself to do so, or calls another function that through one or more nested calls eventually uses lazy-seq to do it. If you call (seq (range 5000)), it returns a "seq" object that you can call first or next/rest on. Calling either of those functions on the seq will not, in general, evaluate the entire sequence, but only part of it, in some cases only the first element, in some cases a few more. (Complication you should probably save for later in your learning process - in the particular case of (seq (range 5)) I believe it will evaluate the entire sequence because of a performance optimization where range returns a "chunked" lazy sequence that produces values in chunks of 32 elements at a time). Andy On Nov 2, 2012, at 7:37 AM, Satoru Logic wrote: > > > On Friday, November 2, 2012 9:57:17 PM UTC+8, Christophe Grand wrote: > Hi, > > A sequence may be (and commonly is) lazy. > Sequences on collections, strings, arrays are not lazy. Nor are those built > with cons. > > So `(seq range(5))` is not lazy, and its contents (0 1 2 3 4) are evaluated > as `seq` returns, right? > > And what makes (count a-seq) slower is that it doesn't keep track of length > of itself, so be it lazy or not, we'll have to traverse it to count it. > > Is it so? > > Some sequences even have a fast (O(1)) count (eg lists, sequences on > strings...) > However you sould assume the worst case which is a traversal of the whole > sequences -- plus if the sequence is lazy (and not yet realized) the > traversal will cause its realization. > > hth, > > Christophe > > > On Fri, Nov 2, 2012 at 1:34 PM, Satoru Logic <sator...@gmail.com> wrote: > Hi, all. > > I read the following description of `sequence` in the book <Clojure > Programming>: > > * Obtaining the length of a seq carries a cost. > * The contents of sequences may be computed lazily and actually realized > only when the value involved are accessed. > > So a sequence is something lazy, right? > > I take it that if we don't do something like `(count a-seq)`, contents of > `a-seq` would not get realized. > > Then if `seq` is already "lazy", what does `lazy-seq` mean? > > -- > You received this message because you are subscribed to the Google > Groups "Clojure" group. > To post to this group, send email to clo...@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+u...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/clojure?hl=en > > > > -- > Professional: http://cgrand.net/ (fr) > On Clojure: http://clj-me.cgrand.net/ (en) > > -- > 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 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