i have a one: (defn lazy-foldr [f coll] (lazy-seq (if-let [[x & xs] coll] (cons x (lazy-foldr f xs)))))
it seems works so far ;-p On Nov 28, 4:01 pm, Laurent PETIT <laurent.pe...@gmail.com> wrote: > Hello, > > clojure is not lazy on evaluation of arguments by default, so I think this > is not possible to do this "transparently". > > Using delay/force could help, but this would not be "transparent" at all for > the writer of the fn ;-) > > 2010/11/27 tpeng <pengt...@gmail.com> > > > thanks Alex for this nice foldr. ;-) > > > however this lazy-foldr can only be used when the fn can stop by > > itself (in this case, it's 'and' which is short-circuited) otherwise > > lazy-foldr will never get stop. > > i think for a good foldr, the evaluation of #(lazy-foldr f val xs) > > should be only forced when necessary? > > > On Nov 27, 6:44 pm, Alex Osborne <a...@meshy.org> wrote: > > > "nicolas.o...@gmail.com" <nicolas.o...@gmail.com> writes: > > > > I doubt there is a foldr that handles the infinite list. > > > > To do anything, it would have to read at least one element: the last. > > > > > Alex nice answer is a foldl. > > > > Actually I think you have them backwards. Wikipedia has a pair of > > > diagrams which I find very useful for visualising which is which: > > > > foldr:http://en.wikipedia.org/wiki/File:Right-fold-transformation.png > > > > foldl (reduce in Clojure): > >http://en.wikipedia.org/wiki/File:Left-fold-transformation.png > > > > Look at the right side of the diagram. That's the call structure. (The > > > left side of the diagram is the linked list (1 2 3 4 5). The ":" just > > > means cons in Haskell syntax.) > > > > With a foldr the outermost call is given the first element of the list, > > > so mine is definitely a foldr. > > > > Let's use (and) as the combining function and use an infinite list that > > > begins [true true false false ...]. So that looks like: > > > > (lazy-foldr #(and %1 (%2)) nil (cycle [true true false false])) > > > > We can draw an ASCII call structure diagram in the same form as the > > > Wikipedia one and see this: > > > > and > > > / \ > > > T and > > > / \ > > > T and > > > / \ > > > F (not evaluated) > > > > Since (and) short-circuits when given false, only the part of the list > > > up to the first false is evaluated. > > > > There's probably not much practical use for lazy-foldr. But it was fun > > > to write. :-) > > > -- > > 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<clojure%2bunsubscr...@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