[Haskell-cafe] Space Leak Help
dominic.steinitz at blueyonder.co.uk
Sun Feb 4 04:45:12 EST 2007
On Sunday 04 February 2007 08:28, Stefan O'Rear wrote:
> On Sun, Feb 04, 2007 at 08:20:23AM +0000, Dominic Steinitz wrote:
> > Someone suggested
> > pad :: Num a => [a] -> [a]
> > pad = pad' 0
> > where pad' !l  = [0x80] ++ ps ++ lb
> > where pl = (64-(l+9)) `mod` 64
> > ps = replicate pl 0x00
> > lb = i2osp 8 (8*l)
> > pad' !l (x:xs) = x : pad' (l+1) xs
> > but that didn't compile
> > *Main> :r
> > [2 of 2] Compiling Main ( allInOne.hs, interpreted )
> > allInOne.hs:83:14: Parse error in pattern
> > Failed, modules loaded: Codec.Utils.
> > Dominic.
> The '!' is a GHC extension, enabled using the flag '-fbang-patterns'.
The test program runs to completion but still has a space leak consuming over
> Equivalently, you can use Haskell 98's "seq" :
> pad :: Num a => [a] -> [a]
> pad = pad' 0
> where pad' l  | l `seq` False = undefined
> pad' l  = [0x80] ++ ps ++ lb
> where pl = (64-(l+9)) `mod` 64
> ps = replicate pl 0x00
> lb = i2osp 8 (8*l)
> pad' l (x:xs) = x : pad' (l+1) xs
> The first alternative never succeeds, but to see that the compiler
> must force the evaluation of 'l'.
dom at heisenberg:~/sha1> ./allInOne 1000001 +RTS -hc -RTS
Stack space overflow: current size 8388608 bytes.
Use `+RTS -Ksize' to increase it.
PS I appreciate all the help I'm getting.
More information about the Haskell-Cafe