« Return to Thread: Stack vs Heap allocation

Re: Stack vs Heap allocation

by Albert Y. C. Lai :: Rate this Message:

Reply to Author | View in Thread

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.

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@...
http://www.haskell.org/mailman/listinfo/haskell-cafe

 « Return to Thread: Stack vs Heap allocation

LightInTheBox - Buy quality products at wholesale price