summaryrefslogtreecommitdiff
path: root/Haskell-book/07/src/Ex.purs
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/07/src/Ex.purs
parentc95abc31d62e296db4f1b537e3de440dd40defd1 (diff)
downloadbook-exercises-3624c712d72d246f21d4e710cec7c11e052e0326.tar.gz
Add the haskell book
Diffstat (limited to 'Haskell-book/07/src/Ex.purs')
-rw-r--r--Haskell-book/07/src/Ex.purs70
1 files changed, 70 insertions, 0 deletions
diff --git a/Haskell-book/07/src/Ex.purs b/Haskell-book/07/src/Ex.purs
new file mode 100644
index 0000000..340917e
--- /dev/null
+++ b/Haskell-book/07/src/Ex.purs
@@ -0,0 +1,70 @@
+module Ex where
+
+import Data.Int (fromString)
+import Data.Maybe (Maybe(..))
+import Data.Tuple (Tuple(..))
+import Prelude
+import Unsafe.Coerce (unsafeCoerce)
+
+--
+-- Multiple choice
+--
+-- Exercise 2
+f :: Char -> String
+f = unsafeCoerce unit
+
+g :: String -> Array String
+g = unsafeCoerce unit
+
+e :: Char -> Array String
+e = g <<< f
+
+-- Exercise 5
+f' :: forall a. a -> a
+f' x = x
+
+g' :: Boolean
+g' = f' true
+
+--
+-- Let's write code
+--
+
+-- Exercise 1
+tensDigit :: Int -> Int
+tensDigit x = d
+ where xLast = x `div` 10
+ d = xLast `mod` 10
+
+-- Exercise 2
+hunsD :: Int -> Int
+hunsD x = d
+ where xLast = x `div` 100
+ d = xLast `mod` 10
+
+foldBool :: forall a. a -> a -> Boolean -> a
+foldBool x y z = case z of
+ false -> x
+ true -> y
+
+findBool2 :: forall a. a -> a -> Boolean -> a
+findBool2 x y z
+ | z = y
+ | otherwise = x
+
+foldBool3 :: forall a. a -> a -> Boolean -> a
+foldBool3 x _ false = x
+foldBool3 _ y true = y
+
+-- Exercise 3
+g'' :: forall a b c. (a -> b) -> Tuple a c -> Tuple b c
+g'' f'' (Tuple a c) = Tuple (f'' a) c
+
+-- Exercise 5
+-- id :: a -> a
+-- id = x = x
+
+roundTrip :: Int -> Int
+roundTrip = fromMaybe <<< fromString <<< show
+ where fromMaybe (Just n) = n
+ fromMaybe Nothing = unsafeCoerce unit