Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is a problem with your <code>average</code>.</p> <pre><code>average a = (sum a) `div` (length a) </code></pre> <p><code>sum</code> uses the entire list. <code>length</code> also uses the entire list. This means that the whole list will be generated and held in memory while one of these functions traverses it, and will not be garbage collected until the other function traverses it.</p> <p>You pass <code>average</code> a very large list, so you will run out of memory.</p> <p>Solution: rewrite <code>average</code> as a function that only traverses the list once, so that the list can be garbage collected as it is generated.</p> <p>Example (untested):</p> <pre><code>average a = sum `div` length where (sum, length) = foldl' f (0, 0) a f (sum, length) i = (sum + i, length + 1) </code></pre> <p>Note that this uses <code>foldl'</code>, from <code>Data.List</code>, not <code>foldl</code>. <code>foldl</code> has its own space issues (which someone more knowledgeable than me may wish to comment on).</p> <p>And as Tobias Wärre has pointed out,</p> <pre><code>roundedSq n | (roundedSq n) == roundedSq (n-1) = n : roundedSq (n+1) </code></pre> <p>will result in an endless loop:</p> <ol> <li>we want to evaluate <code>roundedSq n</code></li> <li>if <code>(roundedSq n) == roundedSq (n-1)</code> is true, we will return <code>n : roundedSq (n+1)</code> as the answer</li> <li>we need to evaluate <code>(roundedSq n) == roundedSq (n-1)</code></li> <li>we need to evaluate <code>roundedSq n</code></li> <li>goto 1.</li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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