Lattice of the endless list of Eratosthenes

Hello, I need to implement a function for the Eratosthenes sieve.

I already have a function

removep p l

which removes elements lthat match the predicate pand function

nats

which returns an endless list of natural numbers, and I have to use both in my solution.

Now I understand how the sieve works, but it seems that I am having problems with this.

I am doing something like this:

sieve = (drop 1 nats)
 where
   sieve (h:t) = h : (removep (\x -> (mod x p) == 0) t) : sieve

but this does not seem to work. Any help? Also note that this is an appointment, so if possible, do not give out the exact solution. I'd rather get an idea of ​​what I'm doing wrong and how I need to change it. Thanks in advance.

+4
2

, nats . removep , .

, , (. Bakuriu) , :

sieve = s (drop 1 nats)
  where
    -- Recursive definition of s here
    -- s (h:t) = ???

, , . .

PS: , . ,

+1

:

  • sieve, , where sieve . sieve , , , error.

    drop 1 nats, .

  • mod x p mod x h, h - , .

  • sieve (h:t) = h:removep ... :_ , h, , removep ..., , sieve ( , .

  • Nitpicking: , mod .

+3

Source: https://habr.com/ru/post/1624148/


All Articles