I decided to get serious with Haskell, and am wrapping my head around the use of foldl and foldr. They really feel a lot like Clojure's reduce - but I might be wrong, and ran into a problem soon enough, which I hope someone will easily explain.
Working with this doc: https://wiki.haskell.org/Foldr_Foldl_Foldl'
Before getting too deep into implementing my own versions of foldr/foldl, I decided to first test the existing ones from Prelude:
± |master U:2 ✗| → ghci
GHCi, version 8.6.3: http://www.haskell.org/ghc/ :? for help
Loaded GHCi configuration from /Users/akarpov/.ghc/ghci.conf
Prelude> foldr (+) 0 [1..9999999]
49999995000000
Prelude> foldr (+) 0 [1..99999999]
*** Exception: stack overflow
Didn't see that coming (same result when using foldl); I rather expected something along the lines of Clojure:
> (time (reduce +' (range 1 99999999)))
"Elapsed time: 3435.638258 msecs"
4999999850000001
The only obvious (and irrelevant) difference is using +' rather than +, but it's only to accommodate the JVM's type system - the number produced doesn't fit into a [default] Long, and +' will automatically use a BigInteger when needed. Most importantly, no stack overflow. It seems to indicate, therefore, that folding/reduction in Haskell/Clojure is either implemented very differently, or my use of haskell's implementation is wrong.
in case it is relevant, these are global-project settings: - packages: [] - resolver: lts-13.8