[Haskell-cafe] Space Leak Help

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


On Sun, Feb 04, 2007 at 08:30:44AM +0000, Dominic Steinitz wrote:
> On Saturday 03 February 2007 19:42, kahl at cas.mcmaster.ca wrote:
> > I would try something along the following lines (untested):
> >
> > \begin{spec}
> > catWithLen xs f = xs ++ f (length xs)
> > \end{spec}
> >
> > \begin{code}
> > catWithLen :: [a] -> (Int -> [a]) -> [a]
> > catWithLen xs f = h 0 xs
> >   where
> >     h k [] = f k
> >     h k (x : xs) = case succ k of            -- forcing evaluation
> >                      k' -> x : h k' xs

Nice try.  k', as a variable binding, is irrefutable.

> > \end{code}
> >
> > \begin{code}
> > pad :: [Word8] -> [Word8]
> > pad xs = catWithLen xs f
> >   where
> >     f l = 0x80 : ps lb
> >       where
> >          -- we know that |l = length xs|
> >          pl = (64-(l+9)) `mod` 64
> >          ps = funPow pl (0x00 :)
> >          lb = i2osp 8 (8*l)
> > \end{code}

> Thanks but this gives a different problem:
> 
> dom at heisenberg:~/sha1> ./allInOne 1000001 +RTS -hc -RTS
> [2845392438,1191608682,3124634993,2018558572,2630932637]
> [2224569924,473682542,3131984545,4182845925,3846598897]
> Stack space overflow: current size 8388608 bytes.
> Use `+RTS -Ksize' to increase it.

expected result of the excessive laziness above.


More information about the Haskell-Cafe mailing list