I have a recursive definition of sieve
in Haskell for prime number computation. But I don’t know how to write the same function using higher–order functions such as map
or filter
. Can anybody show me please?
sieve [] = []
sieve (x:xs) = check (x:xs)
check [] = []
check (x:xs)
|x/=2 && x/=3 && x/=5 && x/=7 = comp (x:xs)
|otherwise = x : sieve xs
comp [] = []
comp (x:xs)
|x `mod` 2 == 0 = sieve xs
|x `mod` 3 == 0 = sieve xs
|x `mod` 5 == 0 = sieve xs
|x `mod` 7 == 0 = sieve xs
|otherwise = x : sieve xs
I threw this together quickly, the speed isn't that great, but it is really easy to implement.