diff options
-rw-r--r-- | lec04/hw.hs | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/lec04/hw.hs b/lec04/hw.hs index 923ec01..c96ef44 100644 --- a/lec04/hw.hs +++ b/lec04/hw.hs @@ -1,3 +1,4 @@ +import Data.List -- Exercise 1 fun1 :: [Integer] -> Integer @@ -48,3 +49,18 @@ map' f = foldr (\x s -> f x : s) [] myFoldl :: (a -> b -> a) -> a -> [b] -> a myFoldl f base xs = foldr (flip f) base (reverse xs) + +-- Exercise 4 + +cartProd :: [a] -> [b] -> [(a, b)] +cartProd xs ys = [(x,y) | x <- xs, y <- ys] + +sieveSundaram :: Integer -> [Integer] +sieveSundaram n = + take (fromInteger n) + . map (\x -> (2*x) + 1) + . (\\) [1..n] + . filter (<=n) + . map (\(i,j) -> i + j + (2*i*j)) + . filter (\(i,j) -> i <= j) + $ cartProd [1..n] [1..n] |