Hello,<br><br>I&#39;m learning Haskell, so I was attempting memoization based upon the Fibonacci examples but for the Ackermann function.&nbsp; In my tests, I found what seems to be truncated output.&nbsp; See my comments at the end of the code for the test cases and output.<br>
<br>### Begin Code ###<br><pre><span class="keyword">module</span> <span class="conid">Main</span> <span class="keyword">where</span><br><br><span class="keyword">import</span> <span class="conid">Data</span><span class="varop">.</span><span class="conid">Array</span><br>
<br><span class="varid">main</span> <span class="keyglyph">=</span> <span class="keyword">do</span><br>  <span class="keyword">let</span> <span class="varid">m</span> <span class="keyglyph">=</span> <span class="num">3</span><br>
      <span class="varid">n</span> <span class="keyglyph">=</span> <span class="num">1</span><br>      <span class="varid">a</span> <span class="keyglyph">=</span> <span class="varid">ackermann_mem</span> <span class="varid">m</span> <span class="varid">n</span><br>
      <span class="varid"></span><span class="varid">putStrLn</span><span class="layout">(</span><span class="str">&quot;Ackermann-mem &quot;</span> <span class="varop">++</span> <span class="varid">show</span> <span class="varid">m</span> <span class="varop">++</span> <span class="str">&quot; &quot;</span> <span class="varop">++</span> <span class="varid">show</span> <span class="varid">n</span> <span class="varop">++</span> <span class="str">&quot; = &quot;</span> <span class="varop">++</span> <span class="varid">show</span> <span class="varid">a</span><span class="layout">)</span><br>
 <br><span class="comment">-- Functions.</span><br>-- Based upon examples from:<br><span class="comment">-- <a href="http://reddit.com/r/programming/info/16ofr/comments%29">http://reddit.com/r/programming/info/16ofr/comments)</a></span><br>
<span class="varid">tabulate</span> <span class="varid">bounds</span> <span class="varid">f</span> <span class="keyglyph">=</span> <span class="varid">array</span> <span class="varid">bounds</span> <span class="keyglyph">[</span><span class="layout">(</span><span class="varid">i</span><span class="layout">,</span> <span class="varid">f</span> <span class="varid">i</span><span class="layout">)</span> <span class="keyglyph">|</span> <span class="varid">i</span> <span class="keyglyph">&lt;-</span> <span class="varid">range</span> <span class="varid">bounds</span><span class="keyglyph">]</span><br>
<span class="varid">dp</span> <span class="varid">bounds</span> <span class="varid">f</span> <span class="keyglyph">=</span> <span class="layout">(</span><span class="varid">memo</span><span class="varop">!</span><span class="layout">)</span> <span class="keyword">where</span> <span class="varid">memo</span> <span class="keyglyph">=</span> <span class="varid">tabulate</span> <span class="varid">bounds</span> <span class="layout">(</span><span class="varid">f</span> <span class="layout">(</span><span class="varid">memo</span><span class="varop">!</span><span class="layout">)</span><span class="layout">)</span><br>
<br><span class="comment"></span><span class="comment"></span><span class="comment">-- Trying to apply memoization function to Ackermann.</span><br><span class="varid">ackermann_mem</span> <span class="varid">m</span> <span class="varid">n</span> <span class="keyglyph">=</span> <span class="varid">dp</span> <span class="layout">(</span><span class="layout">(</span><span class="num">0</span><span class="layout">,</span><span class="num">0</span><span class="layout">)</span><span class="layout">,</span> <span class="layout">(</span><span class="num">30</span><span class="layout">,</span> <span class="num">1000</span><span class="layout">)</span><span class="layout">)</span> <span class="varid">ack</span> <span class="layout">(</span><span class="varid">m</span><span class="layout">,</span> <span class="varid">n</span><span class="layout">)</span><br>
  <span class="keyword">where</span><br>    <span class="varid">ack</span> <span class="varid">rec</span> <span class="layout">(</span><span class="num">0</span><span class="layout">,</span> <span class="varid">n</span><span class="layout">)</span> <span class="keyglyph">=</span> <span class="varid">n</span> <span class="varop">+</span> <span class="num">1</span><br>
    <span class="varid">ack</span> <span class="varid">rec</span> <span class="layout">(</span><span class="varid">m</span><span class="layout">,</span> <span class="num">0</span><span class="layout">)</span> <span class="keyglyph">=</span> <span class="varid">rec</span> <span class="layout">(</span><span class="varid">m</span> <span class="comment">-</span> <span class="num">1</span><span class="layout">,</span> <span class="num">1</span><span class="layout">)</span><br>
    <span class="varid">ack</span> <span class="varid">rec</span> <span class="layout">(</span><span class="varid">m</span><span class="layout">,</span> <span class="varid">n</span><span class="layout">)</span> <span class="keyglyph">=</span> <span class="varid">rec</span> <span class="layout">(</span><span class="varid">m</span> <span class="comment">-</span> <span class="num">1</span><span class="layout">,</span> <span class="varid">rec</span> <span class="layout">(</span><span class="varid">m</span><span class="layout">,</span> <span class="varid">n</span> <span class="comment">-</span> <span class="num">1</span><span class="layout">)</span><span class="layout">)</span><br>
    <span class="comment"></span><br><span class="comment">{-<br>Test cases:<br>ackermann_mem 4 1 = 533 -- when using (30, 1000) as upper bound.<br>ackermann_mem 4 1 = 5533 -- when using (30, 10000) as upper bound. <br>ackermann_mem 4 1 = 65533 -- when using (30, 100000) as upper bound. &lt;--- correct answer!<br>
-}</span></pre>### End Code ###<br><br>It seems if I don&#39;t choose an upper bound pair for (m,n) that is large enough I get truncated output for the answer, instead of GHC giving me an array index exception...&nbsp; This behavior seems very odd to me, can someone explain?&nbsp; Or is this a bug?<br>
<br>Thank you.<br>__<br>Donnie Jones<br>