<div dir="ltr">Hello everyone-<br><br>I'm working on some project Euler problems today, and I'm stuck on one. It's not the problem itself that's the problem, it's that finding the maximum of a list makes me run out of heap space!<br>
<br><a href="http://projecteuler.net/index.php?section=problems&id=14">http://projecteuler.net/index.php?section=problems&id=14</a><br><br>my code:<br><br>import Data.List<br><br>f :: Int -> Int -> Int<br>f acc x<br>
| x == 1 = acc<br> | even x = f (acc + 1) (x `div` 2)<br> | otherwise = f (acc + 1) (3 * x + 1)<br><br>answer = (foldl' max 0) $ map (f 1) [1 .. 999999]<br><br>I tried using 'foldl' max ' instead of 'maximum' because I thought that foldl' was supposed to work better than foldl or something...I could be confused on that point. Anyway, here's what I get...<br>
<br> / _ \ /\ /\/ __(_)<br> / /_\// /_/ / / | | GHC Interactive, version 6.4, for Haskell 98.<br>/ /_\\/ __ / /___| | <a href="http://www.haskell.org/ghc/">http://www.haskell.org/ghc/</a><br>\____/\/ /_/\____/|_| Type :? for help.<br>
<br>Loading package base-1.0 ... linking ... done.<br>Prelude> :l ..\test.hs<br>Compiling Main ( ..\test.hs, interpreted )<br>Ok, modules loaded: Main.<br>*Main> answer<br>GHC's heap exhausted: current limit is 268435456 bytes;<br>
Use the `-M<size>' option to increase the total heap size.<br><br>I also tried writing a simple 'max' function that I thought was tail recursive, and that that would help. Same error. I'm finding it hard to believe that finding a maximum of a list of a million small integers would cause this kind of overflow...is it really that big of a problem?<br>
<br>Any help would be appreciated. I have a feeling I'll run into this problem again in the future.<br></div>