[Haskell-cafe] Space Leak Help

Stefan O'Rear stefanor at cox.net
Sun Feb 4 03:28:26 EST 2007


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'.

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'.


More information about the Haskell-Cafe mailing list