If we try to implement your idea literally we would need one more parameter to the function: the list of existing primes.<br>I&#39;ll call this function primesWRT, since it finds all primes, with respect to the second list provided.<br>
<br><span style="font-family: courier new,monospace;">primesWRT [] _ = []    -- base case</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">primesWRT (x:xs) existing</span><br>
</span><span style="font-family: courier new,monospace;">    -- if all x is not divided by all of the existing primes,<br>    -- x itself is a prime (wrt &#39;existing&#39;)<br>    -- add it to the primes list, and the existing primes list for the next function call<br style="font-family: courier new,monospace;">
</span><span style="font-family: courier new,monospace;">    | all (\ y -&gt; mod x y /= 0 ) existing = x : primesWRT xs (x:existing)</span><span style="font-family: courier new,monospace;"><br>    -- if x is not prime, check the rest.<br>
    | otherwise = primesWRT xs existing</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">primes xs = primesWRT xs []</span><br style="font-family: courier new,monospace;">
<br><br>Please keep in mind that this function assumes its parameter to be in form [2..n]. But this is an assumption coming from your description.<br><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">primes [2..20] = [2,3,5,7,11,13,17,19]</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">primes [2..30] = [2,3,5,7,11,13,17,19,23,29]</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">primes [10..30] = [10,11,12,13,14,15,16,17,18,19,21,23,25,27,29] -- not bad actually, works as advertised.</span><br>
<br>Best,<br><br><div class="gmail_quote">On 12 March 2010 21:14, legajid <span dir="ltr">&lt;<a href="mailto:legajid@free.fr">legajid@free.fr</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
Hi,<br>
i&#39;m trying to write a function to find all primes from 2 to N.<br>
<br>
<br>
My idea is :<br>
   take the first number (2)<br>
   try to find whether it&#39;s a multiple of one of all existing primes ([] at first)<br>
   add 2 to the list of primes<br>
<br>
   take the following number (3)<br>
   find if multiple of existing primes ([2])<br>
   add 3 to the list of primes<br>
<br>
   take the following number (4)<br>
   find if multiple of existing primes ([2, 3])<br>
   do not add 4 to the list of primes<br>
<br>
   ...<br>
<br>
   take the following number (8)<br>
   find if multiple of existing primes ([2, 3, 5, 7])<br>
   do not add 8 to the list of primes<br>
<br>
   take the following number (9)<br>
   find if multiple of existing primes ([2, 3, 5, 7])<br>
   do not add 9 to the list of primes (multiple of 3)<br>
<br>
   and so on...<br>
<br>
So, i would like a function like :<br>
<br>
   f (x : xs)  = g  x  :  f  xs<br>
<br>
g would  return  x  if  x is prime, []  otherwise<br>
<br>
But g would use the preceding value of  f  (list of primes before  the  calculation for x) that is a result of g itself.<br>
f needs g that needs f : what&#39;s wrong with my mind ?<br>
Perhaps i am still under control of imperative programming ?<br>
<br>
Thanks for your help,<br>
<br>
Didier.<br>
<br>
<br>
<br>
<br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
</blockquote></div><br><br clear="all"><br>-- <br>Ozgur Akgun<br>