Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Welcome to the world of lazy evaluation.</p> <p>When you think about it in terms of strict evaluation, foldl looks "good" and foldr looks "bad" because foldl is tail recursive, but foldr would have to build a tower in the stack so it can process the last item first.</p> <p>However, lazy evaluation turns the tables. Take, for example, the definition of the map function:</p> <pre><code>map :: (a -&gt; b) -&gt; [a] -&gt; [b] map _ [] = [] map f (x:xs) = f x : map f xs </code></pre> <p>This wouldn't be too good if Haskell used strict evaluation, since it would have to compute the tail first, then prepend the item (for all items in the list). The only way to do it efficiently would be to build the elements in reverse, it seems.</p> <p>However, thanks to Haskell's lazy evaluation, this map function is actually efficient. Lists in Haskell can be thought of as generators, and this map function generates its first item by applying f to the first item of the input list. When it needs a second item, it just does the same thing again (without using extra space).</p> <p>It turns out that <code>map</code> can be described in terms of <code>foldr</code>:</p> <pre><code>map f xs = foldr (\x ys -&gt; f x : ys) [] xs </code></pre> <p>It's hard to tell by looking at it, but lazy evaluation kicks in because foldr can give <code>f</code> its first argument right away:</p> <pre><code>foldr f z [] = z foldr f z (x:xs) = f x (foldr f z xs) </code></pre> <p>Because the <code>f</code> defined by <code>map</code> can return the first item of the result list using solely the first parameter, the fold can operate lazily in constant space.</p> <p>Now, lazy evaluation does bite back. For instance, try running sum [1..1000000]. It yields a stack overflow. Why should it? It should just evaluate from left to right, right?</p> <p>Let's look at how Haskell evaluates it:</p> <pre><code>foldl f z [] = z foldl f z (x:xs) = foldl f (f z x) xs sum = foldl (+) 0 sum [1..1000000] = foldl (+) 0 [1..1000000] = foldl (+) ((+) 0 1) [2..1000000] = foldl (+) ((+) ((+) 0 1) 2) [3..1000000] = foldl (+) ((+) ((+) ((+) 0 1) 2) 3) [4..1000000] ... = (+) ((+) ((+) (...) 999999) 1000000) </code></pre> <p>Haskell is too lazy to perform the additions as it goes. Instead, it ends up with a tower of unevaluated thunks that have to be forced to get a number. The stack overflow occurs during this evaluation, since it has to recurse deeply to evaluate all the thunks.</p> <p>Fortunately, there is a special function in Data.List called <code>foldl'</code> that operates strictly. <code>foldl' (+) 0 [1..1000000]</code> will not stack overflow. (Note: I tried replacing <code>foldl</code> with <code>foldl'</code> in your test, but it actually made it run slower.)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload