<div dir="ltr">Hi list,<div><br></div><div>I try to get the following little program (a slightly modified "Man or boy", it prints -14254067) work "as expected", that is, without consuming lots of memory:</div><div><br></div><div><div> import <a href="http://Control.Monad.ST">Control.Monad.ST</a></div><div> import Data.STRef</div><div> a k x1 x2 x3 x4 x5 =</div><div>   do kk <- newSTRef k</div><div>      let b = do k <- modifySTRef kk pred >> readSTRef kk; a k b x1 x2 x3 x4</div><div>      if k <= 0 then do x3' <- x3; x4' <- x4; return (x3' + x4')</div><div>                else do x5' <- x5; b' <- b; return (x5' + b')</div><div> main = print (runST (a 22 (return 1) (return (-1)) (return (-1)) (return 1) (return 0)))</div></div><div><br></div><div>I use GHC 7.8.4, and executing this program uses about 2.5 GB of memory (or about 3.5 GB with runghc).  However, the "equivalent" program in OCaml only needs 4 MB (or 9 MB with the interpreter):</div><div><div><br></div><div>let rec a k x1 x2 x3 x4 x5 =</div><div>  let kk = ref k in let rec b () = begin decr kk; a !kk b x1 x2 x3 x4 end</div><div>  in if k <= 0 then let v = x3 () in v + x4 () else let v = x5 () in v + b ();;</div><div>print_int (a 22 (fun () -> 1) (fun () -> -1) (fun () -> -1) (fun () -> 1) (fun () -> 0));; print_newline ();;</div></div><div><br></div><div>Therefore I suspect I'm doing something wrong, but I can't see what.  I did try to use the strict version modifySTRef' as indicated in the manual, but with no visible improvement.</div><div><br></div><div>Thanks,</div><div><br></div><div>Antoine</div><div><br></div></div>