[Haskell-cafe] Expanding do notation

Josef Svenningsson josef.svenningsson at gmail.com
Sat Jan 7 12:23:52 EST 2006


On 1/7/06, Chris Kuklewicz <haskell at list.mightyreason.com> wrote:
>
> When you put "print (head p)" at then end, it keeps a reference to the
> whole list "p" which is your space leak.  If you want to store the head
> of p, this *should* work:
>
> > main = do n <- getArgs >>= return . read . head
> >           let p = permutations [1..n]
> >           headOfP <- return (head p)
> >           mapM_ (putStrLn . concatMap show) $ take 30 p
> >           putStr $ "Pfannkuchen(" ++ show n ++ ") = "
> >           putStrLn . show $ foldl' (flip (max . steps 0)) 0 p
> >           print headOfP
>
> The "headOfP" is computed as an IO action at that point in the program,
> so "headOfP" does not hold a reference to "p" as a whole.


Without having tried this I'd say that you should use
Control.Excection.evaluate instead of 'return'. Or you could use 'seq'. I
suspect that otherwise the 'head p' will not be evaluated until the last
line and you will have the same problem as before.

Cheers,

/Josef
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org//pipermail/haskell-cafe/attachments/20060107/014db07c/attachment.htm


More information about the Haskell-Cafe mailing list