diff options
| author | Eugen Wissner <belka@caraus.de> | 2025-12-11 10:28:11 +0100 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2025-12-11 10:28:11 +0100 |
| commit | 98329e0a3dd4f78b5d815ac3896272ec70904901 (patch) | |
| tree | 80f9c56cfe2ac20232358f236d32e84bd683be1b /Haskell-book/20 | |
| parent | 3624c712d72d246f21d4e710cec7c11e052e0326 (diff) | |
| download | book-exercises-98329e0a3dd4f78b5d815ac3896272ec70904901.tar.gz | |
Add remaining haskell book exercises
Diffstat (limited to 'Haskell-book/20')
| -rw-r--r-- | Haskell-book/20/Exercises.hs | 38 | ||||
| -rw-r--r-- | Haskell-book/20/LibraryFunctions.hs | 51 |
2 files changed, 89 insertions, 0 deletions
diff --git a/Haskell-book/20/Exercises.hs b/Haskell-book/20/Exercises.hs new file mode 100644 index 0000000..6bab580 --- /dev/null +++ b/Haskell-book/20/Exercises.hs @@ -0,0 +1,38 @@ +module Exercises where + +-- 1 +data Constant a b = Constant b deriving (Show) + +instance Foldable (Constant a) where + foldr f acc (Constant x) = f x acc + +-- 2 +data Two a b = Two a b deriving (Show) + +instance Foldable (Two a) where + foldr f acc (Two _ x) = f x acc + +-- 3 +data Three a b c = Three a b c deriving (Show) + +instance Foldable (Three a b) where + foldr f acc (Three _ _ x) = f x acc + +-- 4 +data Three' a b = Three' a b b deriving (Show) + +instance Foldable (Three' a) where + foldr f acc (Three' _ x y) = f y $ f x acc + +-- 5 +data Four' a b = Four' a b b b deriving (Show) + +instance Foldable (Four' a) where + foldr f acc (Four' _ x y z) = f z $ f y $ f x acc + +filterF :: ( Applicative f + , Foldable t + , Monoid (f a)) + => (a -> Bool) -> t a -> f a +filterF f x = foldMap y x + where y k = if f k then pure k else mempty diff --git a/Haskell-book/20/LibraryFunctions.hs b/Haskell-book/20/LibraryFunctions.hs new file mode 100644 index 0000000..412de32 --- /dev/null +++ b/Haskell-book/20/LibraryFunctions.hs @@ -0,0 +1,51 @@ +module LibraryFunctions where + +-- 1 +sum :: (Foldable t, Num a) => t a -> a +sum = foldr (+) 0 + +-- 2 +product :: (Foldable t, Num a) => t a -> a +product = foldr (*) 0 + +-- 3 +elem :: (Foldable t, Eq a) => a -> t a -> Bool +elem needle = foldr (\x b -> b || (needle == x)) False + +-- 4 +minimum :: (Foldable t, Ord a) => t a -> Maybe a +minimum = foldr f Nothing + where f x Nothing = Just x + f x (Just y) + | x > y = Just x + | otherwise = Just y + +-- 5 +maximum :: (Foldable t, Ord a) => t a -> Maybe a +maximum = foldr f Nothing + where f x Nothing = Just x + f x (Just y) + | x < y = Just x + | otherwise = Just y + +-- 6 +null :: (Foldable t) => t a -> Bool +null f = LibraryFunctions.length f == 0 + +-- 7 +length :: (Foldable t) => t a -> Int +length = foldr (\_ l -> l + 1) 0 + +-- 8 +toList :: (Foldable t) => t a -> [a] +toList = foldr (:) [] + +-- 9 +-- | Combine the elements of a structure using a monoid. +-- +fold :: (Foldable t, Monoid m) => t m -> m +fold = LibraryFunctions.foldMap id + +-- 10 +foldMap :: (Foldable t, Monoid m) => (a -> m) -> t a -> m +foldMap f t = foldr (\x m -> mappend (f x) m) mempty t |
