Contrary to what others have stated here, this function does not implement the true sieve of Eratosthenes. It does returns an initial sequence of the prime numbers though, and in a similar manner, so it’s okay to think of it as the sieve of Eratosthenes.
I was about done explaining the code when mipadi posted his answer; I could delete it, but since I spent some time on it, and because our answers are not completely identical, I’ll leave it here.
Firs of all, note that there are some syntax errors in the code you posted. The correct code is,
let sieve (p:xs) = p : sieve (filter (\x -> x `mod` p /= 0) xs) in sieve [2..]
-
let x in y
definesx
and allows its definition to be used iny
. The result of this expression is the result ofy
. So in this case we define a functionsieve
and return the result of applying[2..]
tosieve
. -
Now let us have a closer look at the
let
part of this expression:sieve (p:xs) = p : sieve (filter (\x -> x `mod` p /= 0) xs)
- This defines a function
sieve
which takes a list as its first argument. (p:xs)
is a pattern which matchesp
with the head of said list andxs
with the tail (everything but the head).- In general,
p : xs
is a list whose first element isp
.xs
is a list containing the remaining elements. Thus,sieve
returns the first element of the list it receives. -
Not look at the remainder of the list:
sieve (filter (\x -> x `mod` p /= 0) xs)
- We can see that
sieve
is being called recursively. Thus, thefilter
expression will return a list. filter
takes a filter function and a list. It returns only those elements in the list for which the filter function returns true.-
In this case
xs
is the list being filtered and(\x -> x `mod` p /= 0)
is the filter function.
- The filter function takes a single argument,
x
and returns true iff it is not a multiple ofp
.
- We can see that
- This defines a function
-
Now that
sieve
is defined, we pass it[2 .. ]
, the list of all natural numbers starting at 2. Thus,- The number 2 will be returned. All other natural number which are a multiple of 2 will be discarded.
- The second number is thus 3. It will be returned. All other multiples of 3 will be discarded.
- Thus the next number will be 5. Etc.