Is it not:<br><br>> noLeak :: State Int ()<br>
> noLeak = do<br>
> a <- get<br>
<b>></b> <b> let a' = (a + 1)<br>
> a' `seq` put a'</b><br>
> noLeak<br><br>??<br><br><div class="gmail_quote">2011/6/9 Alexey Khudyakov <span dir="ltr"><<a href="mailto:alexey.skladnoy@gmail.com">alexey.skladnoy@gmail.com</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Hello café!<br>
<br>
This mail is literate haskell<br>
<br>
I have some difficulties with understanding how bang patterns and seq<br>
works.<br>
<br>
> {-# LANGUAGE BangPatterns #-}<br>
> import Control.Monad<br>
> import Control.Monad.Trans.State.Strict<br>
><br>
> leak :: State Int ()<br>
> leak = do<br>
> a <- get<br>
> put (a+1)<br>
> leak<br>
<br>
This function have obvious space leak. It builds huge chain of thunks<br>
so callling `runState leak 0' in ghci will eat all memory. Fix is trivial - add bang pattern. However I couldn't achieve same<br>
effect with seq. How could it be done?<br>
<br>
> noLeak :: State Int ()<br>
> noLeak = do<br>
> a <- get<br>
> let !a' = (a + 1)<br>
> put a'<br>
> noLeak<br>
<br>
<br>
Thanks.<br>
<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</blockquote></div><br>