Skip to content

Instantly share code, notes, and snippets.

@pjb3
Created April 17, 2009 04:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pjb3/96861 to your computer and use it in GitHub Desktop.
Save pjb3/96861 to your computer and use it in GitHub Desktop.
(defn foldr [f acc coll]
(if (first coll)
(f (first coll) (foldr f acc (rest coll)))
[]))
@gnn
Copy link

gnn 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
Copy link

Bill 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