Saturday, March 23, 2019

a haskell implementation of Sieve of Eratosthenes

As a novice of Haskell, tried to implemented a piece of code of Sieve of Eratosthenes.
Quite redundant now, maybe.

prim' :: (Integral a) => [a] -> [a]
prim' []= []
prim' (x:xs) = x : (prim' $ foldl (\acc z -> if (z `mod` x) == 0 then acc else acc ++ [z]) [] xs)

Here is the testing result of it.

> prim' [2..100]
[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]