From 7075a77852b02ff80895201bd13aa48a05404d34 Mon Sep 17 00:00:00 2001 From: Edoardo La Greca Date: Mon, 7 Jul 2025 19:02:37 +0200 Subject: add fourth exercise of lecture 4 --- lec04/hw.hs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'lec04/hw.hs') 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] -- cgit v1.2.3