<div dir="ltr">Hi,<div><br></div><div>First I'll answer your second question:</div><div><br></div><div><span style="font-family:Arial;font-size:13px">but in this case what is i and what is k?</span> <br></div><div>(So the question is about
<font face="monospace">primes 3</font>)</div><div><br></div><div>Think about the meaning of the expression primes 3, which is:</div><div><span style="font-size:13px"><font face="monospace">primes 3 = 2:[i | i <- [3,5..3], and [mod i k /= 0 | k <- primes (isqrt i)]]</font></span></div><div><font face="monospace"> = 2:<b style=""><font color="#000000">[i | i <- [3], and
</font><font color="#ff00ff">[mod i k /= 0 | k <- primes (isqrt i)]</font><font color="#000000">] </font></b></font></div><div>So, in the list
<font face="monospace"><b>[i | i <- <font color="#0000ff">[3]</font>, and
[mod i k /= 0 | k <- primes (isqrt i)]]</b></font> i takes all values in the list <font color="#0000ff"><b>[3]</b></font> (i.e. it is only 3) and for each of them the condition </div><div><font face="monospace"><b><font color="#ff00ff">[mod </font><font color="#cc0000">i</font><font color="#ff00ff"> k /= 0 | k <- primes (isqrt i)] </font></b></font></div><div>must be checked. When i=3, it is
<font face="monospace"><b style=""><font color="#ff00ff">[mod </font><font color="#cc0000">3</font><font color="#ff00ff"> k /= 0 | k <- primes 1]
</font></b></font>
= <font face="monospace"><b style=""><font color="#ff00ff">
[mod </font><font color="#cc0000">3</font><font color="#ff00ff"> k /= 0 | k <- []]</font></b></font>, so the condition is empty (it means you must check that mod 3 k is nonzero for every k in the empty list, so you don't need to check anything!).<br></div><div>So this is clearly
<font face="monospace">primes 3 = 2:<b>[3]</b> = [2,3]</font>.<br></div><div>To understand better, if you had:</div><div><font face="monospace"> [i | i <- [3,4,5,6], and [mod i k /= 0 | k <- []]] </font></div><div>(this never occurs in the program, but just to understand what i and k are)</div><div>i would be respectively 3,4,5 and 6 and for each i, k would be nothing. So the list above is equal to <font face="monospace">[3,4,5,6]</font>.</div><div><br></div><div><br></div><div>Now maybe the meaning of this kind of expressions is mor clear, and you can see that what you imagined the function did is not completely correct:</div><div><font face="monospace"><span style="font-size:13px">primes 10 = 2:[i | i <- [3,5,7,9], and [mod i k /= 0 | k <- primes (isqrt i)]]</span> </font> <br></div><div><br></div><div>So,<font face="monospace"> i</font> is not <font face="monospace">10</font> (so you don't always take k in <font face="monospace">primes 3</font>), since it varies among the values <font face="monospace">3,5,7</font> and <font face="monospace">9</font>.</div><div>When <font face="monospace">i</font> is <font face="monospace">3</font>, as I said before, k is in <font face="monospace">primes 1 = []</font>, so there is no further condition to check, so 3 is in the list.</div><div>When <font face="monospace">i</font> is <font face="monospace">5</font>, <font face="monospace">k</font> is in <font face="monospace">primes (isqrt 5) = primes 2 = [2],</font> so you have to check if <font face="monospace">mod 5 2</font> is nonzero, and it is, so 5 is in the list.</div><div>When <font face="monospace">i</font> is <font face="monospace">7</font>, <font face="monospace">k</font> is in <font face="monospace">primes
(isqrt 7) = primes 2 = [2]</font>, so you have to check if <font face="monospace">mod 7 2</font> is nonzero, and it is, so 7 is in the list.
</div><div>When <font face="monospace">i</font> is <font face="monospace">9</font>,
<font face="monospace">k</font> is in <font face="monospace">primes
(isqrt 9) = primes 3 = [2,3]</font>, so you have to check if <font face="monospace">mod 9 2</font> is nonzero and <font face="monospace">mod 9 3</font> is nonzero, but this is false, so 9 is NOT in the list.
</div><div><br></div><div>As for your first question (which is now about something that happens for <font face="monospace">i=9</font><font face="arial, sans-serif">, not for i=10 which never happens, n=10, not i, so you never look at </font><font face="monospace">primes (isqrt 10), </font><font face="arial, sans-serif">but the answer would be the same, there is no difference)</font></div><div><span style="font-family:Arial;font-size:13px"> I haven't reached base case in this situations so can i or can i not meaningfully evaluate</span><i style="font-family:Arial;font-size:13px"> mod i k /= 0</i><span style="font-family:Arial;font-size:13px">?</span> </div><div><br></div><div>The answer is that of course you have to evaluate <font face="monospace">primes 3</font> before. So when the expression is reduced, first <font face="monospace">primes 3</font> is calculated (and as you said it gives [2,3]), and then mod i k /=0 can be evaluated because you know where both i and k vary.<br></div><div> <br></div><div>I hope it is clear,</div><div>cheers,</div><div>Ut</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Il giorno mer 15 gen 2020 alle ore 15:49 Alexander Chen <<a href="mailto:alexander@chenjia.nl">alexander@chenjia.nl</a>> ha scritto:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="font-family:Arial;font-size:13px">Hi,<div><br></div><div>This could gives you for any given integer the list of prime numbers. source is from a stack overflow snippet.</div><div><br></div><div>helper function:</div><div><div>isqrt :: Integral a => a -> a</div><div>isqrt = floor . sqrt . fromIntegral</div></div><div><br></div><div>main function:</div><div><div>primes 1 = []</div><div>primes n = 2:[i | i <- [3,5..n], and [mod i k /= 0 | k <- primes (isqrt i)]]</div><div><br></div><div><br></div><div><br></div><div>my main unclarity is how I should interpret i and k in de mod part. At every step of the recursion.</div><div><br></div><div>example: <i>primes 10</i>.</div><div><br></div><div>it should generate <i><b>[2,3,5,7,9]</b></i> if you ignore the second part.</div><div><br></div><div>however, when I look at the second part</div><div><br></div><div>then first I need to do this <i>primes (isqrt 10)</i>, which give primes 3. which gives met <b style="font-style:italic">[2,3]</b>. </div><div>first question) I haven't reached base case in this situations so can i or can i not meaningfully evaluate<i> mod i k /= 0</i>?</div><div>Independent of that I now go deeper to reach base case.</div><div>So I go to <i>primes (isqrt 3)</i> which gives me primes 1 which is <b><i>[]</i></b>. Now I hit the base case and need to definitely evaluate <i>mod i k /= 0.</i></div><div>second question) but in this case what is i and what is k?</div><div><br></div><div>If I have it completely wrong, then please be patience. I appreciate the input!</div><div><br></div><div>best,<br><br><div><div style="font-family:Arial;font-size:13px"><br><br></div></div></div></div></div>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
</blockquote></div>