Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You're almost there. While I agree with @koodawg that adding a counter and a running total is another solution for this problem, having those in your code will complicate it more than it needs to be.</p> <p>First, I have a few comments about your code. You must remove the unnecessary parens. <code>hd(numbers)</code> is same as <code>hd numbers</code> and <code>(hd(tl numbers))</code> is equal to <code>hd(tl numbers)</code>. So your <code>(hd(numbers)+(hd(tl numbers)))</code> could be simplified to <code>(hd numbers + hd(tl numbers))</code>. Also, you can combine <code>if null numbers</code> and <code>if hd(numbers) &gt; sum</code> in a single condition for code brevity since they yield the same result: <code>0</code>.</p> <p>I'll try to explain how code works and I hope you'll get the idea where you have to amend your code.</p> <p>Using your example, <code>index(10, [1,2,3,4,5,6,7])</code>, <strong>your code execution will be like this:</strong></p> <p>1) </p> <pre><code> fun index(10, [1,2,3,4,5,6,7]) = if 1 &gt; 10 then 0 else 1 + (10, [1 + 2] append to [2,3,4,5,6,7]) </code></pre> <p><strong>new list:</strong> [3,2,3,4,5,6,7] <br /> <strong>result:</strong> 1 </p> <p>2) </p> <pre><code> fun index(10, [3,2,3,4,5,6,7]) = if 3 &gt; 10 then 0 else 1 + (10, [3 + 2] append to [2,3,4,5,6,7]) </code></pre> <p><strong>new list:</strong> [5,2,3,4,5,6,7] <br /> <strong>result:</strong> 1 </p> <p>3) </p> <pre><code> fun index(10, [5,2,3,4,5,6,7]) = if 5 &gt; 10 then 0 else 1 + (10, [5 + 2] append to [2,3,4,5,6,7]) </code></pre> <p><strong>new list:</strong> [7,2,3,4,5,6,7] <br /> <strong>result:</strong> 1 </p> <p>4) </p> <pre><code> fun index(10, [7,2,3,4,5,6,7]) = if 7 &gt; 10 then 0 else 1 + (10, [7 + 2] append to [2,3,4,5,6,7]) </code></pre> <p><strong>new list:</strong> [9,2,3,4,5,6,7] <br /> <strong>result:</strong> 1 </p> <p>5) </p> <pre><code> fun index(10, [9,2,3,4,5,6,7]) = if 9 &gt; 10 then 0 else 1 + (10, [9 + 2] append to [2,3,4,5,6,7]) </code></pre> <p><strong>new list:</strong> [11,2,3,4,5,6,7] <br /> <strong>result:</strong> 1 </p> <p>6) </p> <pre><code> fun index(10, [11,2,3,4,5,6,7]) = if 11 &gt; 10 then 0 </code></pre> <p><strong>result:</strong> 0 </p> <p>To sum all results: 1 + 1 + 1 + 1 + 1 + 0 = 5 (just like what you said that your function adds 2 to the expected result)</p> <p><strong>The correct code must behave like this:</strong> </p> <p>1) </p> <pre><code> fun index(10, [1,2,3,4,5,6,7]) = if 1 &gt; 10 then 0 else 1 + (10, [1 + 2] append to [3,4,5,6,7]) </code></pre> <p><strong>new list:</strong> [3,3,4,5,6,7] <br /> <strong>result:</strong> 1 </p> <p>2) </p> <pre><code> fun index(10, [3,3,4,5,6,7]) = if 3 &gt; 10 then 0 else 1 + (10, [3 + 3] append to [4,5,6,7]) </code></pre> <p><strong>new list:</strong> [6,4,5,6,7] <br /> <strong>result:</strong> 1 </p> <p>3) </p> <pre><code> fun index(10, [6,4,5,6,7]) = if 6 &gt; 10 then 0 else 1 + (10, [6 + 4] append to [5,6,7]) </code></pre> <p><strong>new list:</strong> [10,5,6,7] <br /> <strong>result:</strong> 1 </p> <p>4) </p> <pre><code> fun index(10, [10,5,6,7]) = if 10 &gt; 10 then 0 </code></pre> <p><strong>result:</strong> 0 </p> <p>To sum all results: 1 + 1 + 1 + 0 = 3 which is the expected answer. </p> <p><strong>HINT:</strong> You always make sure that the new list your function is processing must be smaller than the previous list/original list. </p> <p>I hope I explained clearly why your code isn't working. I didn't include the code because I know this is a homework for an online class.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload