Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Let us start by looking at the <code>divisors</code> function. It is sort of correct, with only two issues:</p> <ol> <li><p>What is the <code>xs</code> argument supposed to be? From the definition, it looks like it should be all the prime numbers below <code>x</code> - let's call these the candidate primes. So if <code>x</code> was <code>10</code>, then the candidate primes should be <code>[2,3,5,7]</code>. However, this is not what the function gets as an argument. In your code, <code>xs</code> is the infinite list of primes.</p></li> <li><p>Technically, the <code>divisors</code> doesn't return all divisors. <code>divisors 16 [2,3,5,7,11,13]</code> wouldn't return <code>8</code>, for instance. But this is a minor nitpick.</p></li> </ol> <p>So if we can call <code>divisors</code> with the right list of primes, then we should be ok, and the <code>isPrime</code> function would also be fine. </p> <p>The problem is getting the list of candidate primes. For clarity, I will give the code first, and then explain:</p> <pre><code>primeLst = 2 : [ x | x &lt;- [3..], isPrime x (takeWhile (\p -&gt; p*p &lt;= x) primeLst)] </code></pre> <p>I have made two changes:</p> <ol> <li><p>I made sure that <code>primeLst</code> includes <code>2</code>, by sticking it at the front. </p></li> <li><p>I restricted the candidate primes by taking numbers from the infinite list of primes until I reached a number that was higher than the square root of the number I was testing for primeness. In doing this i changed the definition of the candidate primes slightly, so for instance the candidates for <code>26</code> are <code>[2,3,5]</code> instead of <code>[2,3,5,7,11,13,17,19,23]</code>. But it still works.</p></li> </ol> <p>Two questions for you to think about: </p> <ol> <li><p>Why does it still work with the new definition of candidate primes?</p></li> <li><p>Why doesn't the following line of code work, even though it seems it should give the original definition of the candidate primes?</p></li> </ol> <p>:</p> <pre><code>primeLst = 2 : [ x | x &lt;- [3..], isPrime x (takeWhile (\p -&gt; p &lt; x) primeLst)] </code></pre> <p>The last question is hard, so if you have questions, post them in the comments.</p>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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