tpeng <pengt...@gmail.com> writes: >> (defn foldr [f coll] >> (reduce #(f %2 %1) (reverse coll)))
> but this foldr can't handle the infinite list, am i right? Correct. In a lazily evaluated language like Haskell you can have a combining function which doesn't always evaluate its arguments and thus only partially uses the list. Clojure is eagerly evaluated, so we can't do it quite the same way. We can of course achieve much the same effect by explicitly delaying evaluation using closures. This is how lazy-seq works. For example, if we make the second argument to the combining function a closure: (defn lazy-foldr [f val coll] (if-let [[x & xs] coll] (f x #(lazy-foldr f val xs)) val)) ;; Finite seq of trues (lazy-foldr #(and %1 (%2)) true [true true]) ;; => true ;; Infinite seq of falses (lazy-foldr #(and %1 (%2)) true (repeat false)) ;; => false -- 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