summaryrefslogtreecommitdiff
path: root/Haskell-book/10/Exercise.hs
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2025-12-09 16:32:32 +0100
committerEugen Wissner <belka@caraus.de>2025-12-09 16:32:32 +0100
commit3624c712d72d246f21d4e710cec7c11e052e0326 (patch)
treef385cb51c72a0c5eeb2057609b75f5f8c6c4f272 /Haskell-book/10/Exercise.hs
parentc95abc31d62e296db4f1b537e3de440dd40defd1 (diff)
downloadbook-exercises-3624c712d72d246f21d4e710cec7c11e052e0326.tar.gz
Add the haskell book
Diffstat (limited to 'Haskell-book/10/Exercise.hs')
-rw-r--r--Haskell-book/10/Exercise.hs53
1 files changed, 53 insertions, 0 deletions
diff --git a/Haskell-book/10/Exercise.hs b/Haskell-book/10/Exercise.hs
new file mode 100644
index 0000000..8207e5e
--- /dev/null
+++ b/Haskell-book/10/Exercise.hs
@@ -0,0 +1,53 @@
+module Exercise where
+
+stops :: String
+stops = "pbtdkg"
+
+vowels :: String
+vowels = "aeiou"
+
+stopVowelStop :: [(Char, Char, Char)]
+stopVowelStop = filter p [(x, y, z) | x <- stops, y <- vowels, z <- stops]
+ where p (a, _, _) = a == 'p'
+
+seekritFunc :: String -> Double
+seekritFunc x = (/) (fromIntegral (sum (map length (words x)))) (fromIntegral (length (words x)))
+
+myAnd :: [Bool] -> Bool
+myAnd = foldr (&&) True
+
+myOr :: [Bool] -> Bool
+myOr = foldr (||) False
+
+myAny :: (a -> Bool) -> [a] -> Bool
+myAny f = myOr . map f
+
+myElem :: Eq a => a -> [a] -> Bool
+myElem x = foldr (\y z -> z || (y == x)) False
+
+myElem' :: Eq a => a -> [a] -> Bool
+myElem' needle = any (\x -> x == needle)
+
+myReverse :: [a] -> [a]
+myReverse = foldl (flip (:)) []
+
+myMap :: (a -> b) -> [a] -> [b]
+myMap f = foldr (\x y -> (f x) : y) []
+
+myFilter :: (a -> Bool) -> [a] -> [a]
+myFilter f = foldr (\x xs -> if f x then x : xs else xs) []
+
+squish :: [[a]] -> [a]
+squish = foldr (++) []
+
+squishMap :: (a -> [b]) -> [a] -> [b]
+squishMap f xs = foldr (++) [] (map f xs)
+
+squishAgain :: [[a]] -> [a]
+squishAgain = squishMap id
+
+myMaximumBy :: (a -> a -> Ordering) -> [a] -> a
+myMaximumBy f (z:zs) = foldl (\x y -> if (f x y) == GT then x else y) z zs
+
+myMinimumBy :: (a -> a -> Ordering) -> [a] -> a
+myMinimumBy f (z:zs) = foldl (\x y -> if (f x y) == LT then x else y) z zs