[Haskell-cafe] Stack vs Heap allocation

Albert Y. C. Lai trebla at vex.net
Thu May 8 19:13:08 EDT 2008


Edsko de Vries wrote:
> sum :: Tree -> Int
> sum t = sum' [t] 0
>   where
>     sum' [] acc = acc
>     sum' (Leaf i : ts) acc = sum' ts $! (i + acc)
>     sum' (Node l r : ts) acc = sum' (l : r : ts) acc

Because of $!, you should compare the Leaf case to foldl', not foldl.

The Node case can be said to mimic a stack using heap resource.



More information about the Haskell-Cafe mailing list