From 5adf189982080ac960af73cf6bbfa9f973ed5910 Mon Sep 17 00:00:00 2001 From: Ramakrishnan Muthukrishnan <ram@rkrishnan.org> Date: Sat, 20 Dec 2014 12:50:36 +0530 Subject: [PATCH] hw4: exercise 3 - implement map in terms of fold --- hw4/hw4.hs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/hw4/hw4.hs b/hw4/hw4.hs index 780ee46..455d8b9 100644 --- a/hw4/hw4.hs +++ b/hw4/hw4.hs @@ -68,4 +68,16 @@ foldTree :: [a] -> Tree a foldTree = foldr f Leaf where f x acc = insert x acc - +-- exercise 3 +-- xor +xor :: [Bool] -> Bool +xor bs = foldr f False bs + where f False False = False + f False True = True + f True False = True + f True True = False + +-- implement map as a fold +map' :: (a -> b) -> [a] -> [b] +map' f = foldr f' [] + where f' x acc = (f x) : acc -- 2.45.2