summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdoardo La Greca2025-07-07 19:02:37 +0200
committerEdoardo La Greca2025-07-07 19:02:37 +0200
commit7075a77852b02ff80895201bd13aa48a05404d34 (patch)
tree211d78576086ea3bb670ab09aa95e7ad74fd8149
parent871d3761cb1d12c593ad3a4aa059cc23ae35f87a (diff)
add fourth exercise of lecture 4
-rw-r--r--lec04/hw.hs16
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]