Add the haskell book

This commit is contained in:
2025-12-09 16:32:32 +01:00
parent c95abc31d6
commit 3624c712d7
67 changed files with 1576 additions and 0 deletions

8
Haskell-book/07/.gitignore vendored Normal file
View File

@@ -0,0 +1,8 @@
/bower_components/
/node_modules/
/.pulp-cache/
/output/
/generated-docs/
/.psc*
/.purs*
/.psa*

View File

@@ -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"
}
}

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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")

View File

@@ -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)

View File

@@ -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."