diff options
| author | Eugen Wissner <belka@caraus.de> | 2025-12-09 16:32:32 +0100 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2025-12-09 16:32:32 +0100 |
| commit | 3624c712d72d246f21d4e710cec7c11e052e0326 (patch) | |
| tree | f385cb51c72a0c5eeb2057609b75f5f8c6c4f272 /Haskell-book/07 | |
| parent | c95abc31d62e296db4f1b537e3de440dd40defd1 (diff) | |
| download | book-exercises-3624c712d72d246f21d4e710cec7c11e052e0326.tar.gz | |
Add the haskell book
Diffstat (limited to 'Haskell-book/07')
| -rw-r--r-- | Haskell-book/07/.gitignore | 8 | ||||
| -rw-r--r-- | Haskell-book/07/bower.json | 19 | ||||
| -rw-r--r-- | Haskell-book/07/src/ArtfulDodgy.purs | 13 | ||||
| -rw-r--r-- | Haskell-book/07/src/CasePractice.purs | 24 | ||||
| -rw-r--r-- | Haskell-book/07/src/Ex.purs | 70 | ||||
| -rw-r--r-- | Haskell-book/07/src/GrabBag.purs | 14 | ||||
| -rw-r--r-- | Haskell-book/07/src/Main.purs | 9 | ||||
| -rw-r--r-- | Haskell-book/07/src/VarietyPack.purs | 9 | ||||
| -rw-r--r-- | Haskell-book/07/test/Main.purs | 9 |
9 files changed, 175 insertions, 0 deletions
diff --git a/Haskell-book/07/.gitignore b/Haskell-book/07/.gitignore new file mode 100644 index 0000000..9623fa5 --- /dev/null +++ b/Haskell-book/07/.gitignore @@ -0,0 +1,8 @@ +/bower_components/ +/node_modules/ +/.pulp-cache/ +/output/ +/generated-docs/ +/.psc* +/.purs* +/.psa* diff --git a/Haskell-book/07/bower.json b/Haskell-book/07/bower.json new file mode 100644 index 0000000..3f3e066 --- /dev/null +++ b/Haskell-book/07/bower.json @@ -0,0 +1,19 @@ +{ + "name": "07", + "ignore": [ + "**/.*", + "node_modules", + "bower_components", + "output" + ], + "dependencies": { + "purescript-prelude": "^3.1.0", + "purescript-console": "^3.0.0", + "purescript-integers": "^3.1.0", + "purescript-tuples": "^4.1.0", + "purescript-unsafe-coerce": "^3.0.0" + }, + "devDependencies": { + "purescript-psci-support": "^3.0.0" + } +} diff --git a/Haskell-book/07/src/ArtfulDodgy.purs b/Haskell-book/07/src/ArtfulDodgy.purs new file mode 100644 index 0000000..5206c8d --- /dev/null +++ b/Haskell-book/07/src/ArtfulDodgy.purs @@ -0,0 +1,13 @@ +module ArtfulDodgy where + +import Prelude + +-- Exercise 1 +dodgy :: Int -> Int -> Int +dodgy x y = x + y * 10 + +oneIsOne :: Int -> Int +oneIsOne = dodgy 1 + +oneIsTwo :: Int -> Int +oneIsTwo = (flip dodgy) 2 diff --git a/Haskell-book/07/src/CasePractice.purs b/Haskell-book/07/src/CasePractice.purs new file mode 100644 index 0000000..8f8cbab --- /dev/null +++ b/Haskell-book/07/src/CasePractice.purs @@ -0,0 +1,24 @@ +module CasePractice where + +import Data.Int (even) +import Prelude + +-- Exercise 1 +functionC :: forall a. Ord a => a -> a -> a +functionC x y = case (x > y) of + true -> x + false -> y + +-- Exercise 2 +isEvenAdd2 :: Int -> Int +isEvenAdd2 n = case even n of + true -> n + 2 + false -> n + +-- Exercise 3 +nums :: Int -> Int +nums x = + case compare x 0 of + LT -> -1 + GT -> 1 + EQ -> 0 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 diff --git a/Haskell-book/07/src/GrabBag.purs b/Haskell-book/07/src/GrabBag.purs new file mode 100644 index 0000000..2967002 --- /dev/null +++ b/Haskell-book/07/src/GrabBag.purs @@ -0,0 +1,14 @@ +module GrabBags where + +import Data.Int (odd) +import Prelude + +-- 3 a) +addOneIfOdd :: Int -> Int +addOneIfOdd n = case odd n of + true -> f n + false -> n + where f = \k -> k + 1 + +addFive :: (Int -> (Int -> Int)) +addFive = \x -> \y -> (if x > y then y else x) + 5 diff --git a/Haskell-book/07/src/Main.purs b/Haskell-book/07/src/Main.purs new file mode 100644 index 0000000..836cae9 --- /dev/null +++ b/Haskell-book/07/src/Main.purs @@ -0,0 +1,9 @@ +module Main where + +import Prelude +import Control.Monad.Eff (Eff) +import Control.Monad.Eff.Console (CONSOLE, log) + +main :: forall e. Eff (console :: CONSOLE | e) Unit +main = do + log (id "4") diff --git a/Haskell-book/07/src/VarietyPack.purs b/Haskell-book/07/src/VarietyPack.purs new file mode 100644 index 0000000..602ae8d --- /dev/null +++ b/Haskell-book/07/src/VarietyPack.purs @@ -0,0 +1,9 @@ +module VarietyPack where + +import Data.Tuple (Tuple(..)) + +f :: forall a b c d e g + . Tuple (Tuple a b) c + -> Tuple (Tuple d e) g + -> Tuple (Tuple a d) (Tuple c g) +f (Tuple (Tuple a b) c) (Tuple (Tuple d e) g) = Tuple (Tuple a d) (Tuple c g) diff --git a/Haskell-book/07/test/Main.purs b/Haskell-book/07/test/Main.purs new file mode 100644 index 0000000..845d0f4 --- /dev/null +++ b/Haskell-book/07/test/Main.purs @@ -0,0 +1,9 @@ +module Test.Main where + +import Prelude +import Control.Monad.Eff (Eff) +import Control.Monad.Eff.Console (CONSOLE, log) + +main :: forall e. Eff (console :: CONSOLE | e) Unit +main = do + log "You should add some tests." |
