[Haskell-cafe] Space Leak Help

kahl at cas.mcmaster.ca kahl at cas.mcmaster.ca
Sun Feb 4 11:59:09 EST 2007


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


Does it still do that if you youse seq instead of case?


\begin{code}
catWithLen :: [a] -> (Int -> [a]) -> [a]
catWithLen xs f = h 0 xs
  where
    h k [] = f k
    h k (x : xs) =  let k' = succ k
                    in k' `seq` x : h k' xs
\end{code}


Wolfram


More information about the Haskell-Cafe mailing list