From ef86ec4a73d7a82bf616300201769af3567a3a11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Depreeuw?= Date: Sun, 5 Nov 2017 11:05:36 +0100 Subject: [PATCH] Complete chapter 20 --- 20-foldable/20.5-library-functions.md | 2 ++ 20-foldable/20.6-chapter-exercises.md | 2 ++ 20-foldable/src/chapter.hs | 50 +++++++++++++++++++++++++++ 20-foldable/src/functions.hs | 49 ++++++++++++++++++++++++++ 4 files changed, 103 insertions(+) create mode 100644 20-foldable/20.5-library-functions.md create mode 100644 20-foldable/20.6-chapter-exercises.md create mode 100644 20-foldable/src/chapter.hs create mode 100644 20-foldable/src/functions.hs diff --git a/20-foldable/20.5-library-functions.md b/20-foldable/20.5-library-functions.md new file mode 100644 index 0000000..bdab0f8 --- /dev/null +++ b/20-foldable/20.5-library-functions.md @@ -0,0 +1,2 @@ +# Exercises: Library Functions +see src/functions.h \ No newline at end of file diff --git a/20-foldable/20.6-chapter-exercises.md b/20-foldable/20.6-chapter-exercises.md new file mode 100644 index 0000000..02acf4a --- /dev/null +++ b/20-foldable/20.6-chapter-exercises.md @@ -0,0 +1,2 @@ +# Chapter Exercises +see src/chapter.hs \ No newline at end of file diff --git a/20-foldable/src/chapter.hs b/20-foldable/src/chapter.hs new file mode 100644 index 0000000..f7fa60c --- /dev/null +++ b/20-foldable/src/chapter.hs @@ -0,0 +1,50 @@ +module Chapter where + +-- foldMap :: (Monoid m, Foldable t) => (a -> m) -> t a -> m +-- foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b + +-- I will implement both foldr and foldMap even though only 1 is necessary + +-- 1 +data Constant a b = Constant b deriving (Eq, Show) +instance Foldable (Constant a) where + foldr f z (Constant b) = f b z + foldMap f (Constant b) = f b +-- 1' Mistake in book, probably meant: +data Constant' a b = Constant' a deriving (Eq, Show) +instance Foldable (Constant' a) where + foldr _ z _ = z + foldMap _ _ = mempty + +-- 2 +data Two a b = Two a b deriving (Eq, Show) +instance Foldable (Two a) where + foldr f z (Two _ b) = f b z + foldMap f (Two _ b) = f b + +-- 3 +data Three a b c = Three a b c deriving (Eq, Show) +instance Foldable (Three a b) where + foldr f z (Three _ _ c) = f c z + foldMap f (Three _ _ c) = f c + +-- 4 +data Three' a b = Three' a b b deriving (Eq, Show) +instance Foldable (Three' a) where + foldr f z (Three' _ b b') = f b $ f b' z + foldMap f (Three' _ b b') = (f b) `mappend` (f b') + +-- 5 +data Four' a b = Four' a b b b deriving (Eq, Show) +instance Foldable (Four' a) where + foldr f z (Four' _ b1 b2 b3) = f b1 $ f b2 $ f b3 z + foldMap f (Four' _ b1 b2 b3) = (f b1) `mappend` (f b2) `mappend` (f b3) + + +filterF :: (Applicative f, Foldable t, Monoid (f a)) + => (a -> Bool) -> t a -> f a +filterF f = foldr (\a b -> if f a then pure a `mappend` b else b) mempty + +filterF' :: (Applicative f, Foldable t, Monoid (f a)) + => (a -> Bool) -> t a -> f a +filterF' f = foldMap (\a -> if f a then pure a else mempty) \ No newline at end of file diff --git a/20-foldable/src/functions.hs b/20-foldable/src/functions.hs new file mode 100644 index 0000000..04e3ab5 --- /dev/null +++ b/20-foldable/src/functions.hs @@ -0,0 +1,49 @@ +module Functions where + +import Data.Monoid + +-- foldMap :: (Monoid m, Foldable t) => (a -> m) -> t a -> m +-- foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b + +sum' :: (Foldable t, Num a) => t a -> a +-- sum' = foldr (+) 0 +sum' = getSum . foldMap Sum + +product' :: (Foldable t, Num a) => t a -> a +-- product' = foldr (*) 1 +product' = getProduct . foldMap Product + +elem' :: (Foldable t, Eq a) => a -> t a -> Bool +-- elem' e = foldr (\a b -> a == e || b) False +elem' e = getAny . (foldMap (Any . (==e))) + +minimum' :: (Foldable t, Ord a) => t a -> Maybe a +minimum' = foldr (\a b -> + case b of + Nothing -> Just a + Just a' -> Just $ min a a') + Nothing + +maximum' :: (Foldable t, Ord a) => t a -> Maybe a +maximum' = foldr (\a b -> + case b of + Nothing -> Just a + Just a' -> Just $ max a a') + Nothing + +null' :: Foldable t => t a -> Bool +null' = foldr (\_ _ -> False) True + +length' :: Foldable t => t a -> Int +length' = foldr (\_ b -> b + 1) 0 + +toList' :: Foldable t => t a -> [a] +toList' = foldr (:) [] + +fold' :: (Foldable t, Monoid m) => t m -> m +-- fold' = foldMap (mappend mempty) -- mappend mempty = id +fold' = foldMap id + +foldMap' :: (Foldable t, Monoid m) => (a -> m) -> t a -> m +-- foldMap' f = foldr (\a b -> mappend (f a) b) mempty +foldMap' f = foldr (mappend . f) mempty \ No newline at end of file