parent
c11f9bbf3a
commit
ef86ec4a73
4 changed files with 103 additions and 0 deletions
@ -0,0 +1,2 @@ |
|||||||
|
# Exercises: Library Functions |
||||||
|
see src/functions.h |
@ -0,0 +1,2 @@ |
|||||||
|
# Chapter Exercises |
||||||
|
see 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) |
@ -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 |
Loading…
Reference in new issue