1
0

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/08/.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,22 @@
{
"name": "08",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"output"
],
"dependencies": {
"purescript-prelude": "^3.1.0",
"purescript-console": "^3.0.0",
"purescript-tuples": "^4.1.0",
"purescript-generics-rep": "^5.3.0",
"purescript-arrays": "^4.2.1",
"purescript-foldable-traversable": "^3.6.1",
"purescript-unfoldable": "^3.0.0",
"purescript-maybe": "^3.0.0"
},
"devDependencies": {
"purescript-psci-support": "^3.0.0"
}
}

View File

@@ -0,0 +1,72 @@
module Exercises where
import Data.Tuple (Tuple(..), fst, snd)
import Prelude
--
-- Reviewing currying
--
cattyConny :: String -> String -> String
cattyConny x y = x <> " mrow " <> y
flippy :: String -> String -> String
flippy = flip cattyConny
appedCatty :: String -> String
appedCatty = cattyConny "woops"
frappe :: String -> String
frappe = flippy "haha"
--
-- Recursion
--
-- Exercise 2
sumNumbers :: Int -> Int
sumNumbers x = sumNumbers' x 0
where sumNumbers' 0 acc = 0
sumNumbers' n acc = n + (sumNumbers' (n - 1) acc)
-- Exercise 2
multiply :: Int -> Int -> Int
multiply _ 0 = 0
multiply x 1 = x
multiply x y = x + multiply x (y - 1)
--
-- Fixing dividedBy
--
data DividedResult =
Result Int
| DividedByZero
instance showDividedResult :: Show DividedResult where
show :: DividedResult -> String
show (Result n) = show n
show DividedByZero = show "Divided by zero"
go :: Int -> Int -> Int -> Tuple Int Int
go n d count
| n < d = Tuple count n
| otherwise = go (n - d) d (count + 1)
abs :: Int -> Int
abs x
| x < 0 = -x
| otherwise = x
dividedBy :: Int -> Int -> Tuple DividedResult DividedResult
dividedBy num denom
| denom == 0 = Tuple DividedByZero DividedByZero
| (num * denom) < 0 = let tuple = go (abs num) (abs denom) 0 in
Tuple (Result (-(fst tuple))) (Result (snd tuple))
| otherwise = let tuple = go (abs num) (abs denom) 0 in
Tuple (Result (fst tuple)) (Result (snd tuple))
--
-- McCarthy 91 function
--
mc91 :: Int -> Int
mc91 n
| n > 100 = n - 10
| otherwise = mc91 (mc91 (n + 11))

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 "Hello sailor!"

View File

@@ -0,0 +1,30 @@
module WordNumber where
import Data.Array (reverse)
import Data.Maybe (Maybe(..))
import Data.String (joinWith)
import Data.Tuple (Tuple(..))
import Data.Unfoldable (unfoldr)
import Prelude
digitToWord :: Int -> String
digitToWord 0 = "null"
digitToWord 1 = "one"
digitToWord 2 = "two"
digitToWord 3 = "three"
digitToWord 4 = "four"
digitToWord 5 = "five"
digitToWord 6 = "six"
digitToWord 7 = "seven"
digitToWord 8 = "eight"
digitToWord 9 = "nine"
digitToWord _ = ""
digits :: Int -> Array Int
digits n = reverse $ unfoldr unfold n
where unfold x
| x == 0 = Nothing
| otherwise = Just (Tuple (mod x 10) (div x 10))
wordNumber :: Int -> String
wordNumber n = joinWith "-" $ map digitToWord (digits n)

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