From: Ramakrishnan Muthukrishnan Date: Sat, 20 Dec 2014 07:20:36 +0000 (+0530) Subject: hw4: exercise 3 - implement map in terms of fold X-Git-Url: https://git.rkrishnan.org/components/com_hotproperty/simplejson/%5B%5E?a=commitdiff_plain;h=5adf189982080ac960af73cf6bbfa9f973ed5910;p=yorgey.git hw4: exercise 3 - implement map in terms of fold --- 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