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