You could define a naive 'foldr' like this:
(defn foldr [f coll]
  (if (empty? (rest coll))
    (first coll)
    (f (first coll) (foldr f (rest coll)))) )

(foldr list '(1 2 3 4)) => (1 (2 (3 4)))
(foldr - [1 2 3 4]) => -2

However, this is not a tail-recursive function and will be limited by the size 
of the stack.

An alternative involves reversing the sequence and applying the function with 
its arguments reversed. Then we can just pretend that we are doing a left fold 
and use 'reduce':
(defn foldr [f coll]
  (reduce (fn [x y] (f y x)) (reverse coll)))

Or for those of you who prefer that other people won't be able to read your 
code:
(defn foldr [f coll]
  (reduce #(f %2 %1) (reverse coll)))

Have all good days,
David Sletten

On Nov 5, 2010, at 10:03 PM, Yang Dong wrote:

> Maybe because Clojure has a vector, and conj conjoins new elements to
> the end of the vector, so there's mere little use of fold-right. But,
> fold-right is an abstraction tool, missing it in the core is kind of
> pity.
> 
> -- 
> 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

Reply via email to