Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(defn foldr [f acc coll]
(if (first coll)
(f (first coll) (foldr f acc (rest coll)))
[]))
@gnn

This comment has been minimized.

Copy link

commented Jan 13, 2011

Stumbled upon this after googling "clojure foldr". I realise this gist is quite old, so I apologise in advance if you feel this is pointless.
The last line should be:

acc))
@Bill

This comment has been minimized.

Copy link

commented Aug 13, 2017

With the change, it is a right-associative fold:

(foldr list () (range 3))
=> (0 (1 (2 ())))

But it isn't lazy like Haskell fold:

(take 2 (foldr list () (range)))

StackOverflowError   clojure.lang.RT.first (RT.java:682)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.