summaryrefslogtreecommitdiff
path: root/lec04/hw.hs
diff options
context:
space:
mode:
authorEdoardo La Greca2025-07-07 17:26:34 +0200
committerEdoardo La Greca2025-07-07 17:39:12 +0200
commit871d3761cb1d12c593ad3a4aa059cc23ae35f87a (patch)
tree3a40d12bd8a8a71f30981c8d08ae07641c3f4526 /lec04/hw.hs
parenta78fefdfcb3522499a1508927795d0f2c9bf12a6 (diff)
add third exercise of lecture 4
Diffstat (limited to 'lec04/hw.hs')
-rw-r--r--lec04/hw.hs11
1 files changed, 11 insertions, 0 deletions
diff --git a/lec04/hw.hs b/lec04/hw.hs
index bb5a026..923ec01 100644
--- a/lec04/hw.hs
+++ b/lec04/hw.hs
@@ -37,3 +37,14 @@ treeInsertBalanced t@(Node h tl@(Node hl _ _ _) e tr@(Node hr _ _ _)) a
foldTree :: [a] -> Tree a
foldTree = foldr (flip treeInsertBalanced) Leaf
+
+-- Exercise 3
+
+xor :: [Bool] -> Bool
+xor = odd . foldl (\s x -> if x then s+1 else s) 0
+
+map' :: (a -> b) -> [a] -> [b]
+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)