Created
April 17, 2009 04:43
-
-
Save pjb3/96861 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn foldr [f acc coll] | |
(if (first coll) | |
(f (first coll) (foldr f acc (rest coll))) | |
[])) |
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
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: