summaryrefslogtreecommitdiff
path: root/Haskell-book/06
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/06
parentc95abc31d62e296db4f1b537e3de440dd40defd1 (diff)
downloadbook-exercises-3624c712d72d246f21d4e710cec7c11e052e0326.tar.gz
Add the haskell book
Diffstat (limited to 'Haskell-book/06')
-rw-r--r--Haskell-book/06/.gitignore8
-rw-r--r--Haskell-book/06/bower.json16
-rw-r--r--Haskell-book/06/src/EqInstances.purs63
-rw-r--r--Haskell-book/06/src/Ex.purs9
-rw-r--r--Haskell-book/06/src/Main.purs30
-rw-r--r--Haskell-book/06/test/Main.purs9
6 files changed, 135 insertions, 0 deletions
diff --git a/Haskell-book/06/.gitignore b/Haskell-book/06/.gitignore
new file mode 100644
index 0000000..9623fa5
--- /dev/null
+++ b/Haskell-book/06/.gitignore
@@ -0,0 +1,8 @@
+/bower_components/
+/node_modules/
+/.pulp-cache/
+/output/
+/generated-docs/
+/.psc*
+/.purs*
+/.psa*
diff --git a/Haskell-book/06/bower.json b/Haskell-book/06/bower.json
new file mode 100644
index 0000000..f4358bf
--- /dev/null
+++ b/Haskell-book/06/bower.json
@@ -0,0 +1,16 @@
+{
+ "name": "06",
+ "ignore": [
+ "**/.*",
+ "node_modules",
+ "bower_components",
+ "output"
+ ],
+ "dependencies": {
+ "purescript-prelude": "^3.1.0",
+ "purescript-console": "^3.0.0"
+ },
+ "devDependencies": {
+ "purescript-psci-support": "^3.0.0"
+ }
+}
diff --git a/Haskell-book/06/src/EqInstances.purs b/Haskell-book/06/src/EqInstances.purs
new file mode 100644
index 0000000..987d273
--- /dev/null
+++ b/Haskell-book/06/src/EqInstances.purs
@@ -0,0 +1,63 @@
+module EqInstances where
+ -- Exercise: Eq Instances
+ -- p. 178
+
+import Prelude
+
+-- Ex. 1
+data TisAnInteger =
+ TisAn Int
+
+instance eqTisAn :: Eq TisAnInteger where
+ eq x y = x == y
+
+-- Ex. 2
+data TwoIntegers =
+ Two Int Int
+
+instance ewTwoIntegers :: Eq TwoIntegers where
+ eq (Two x y) (Two x' y') = x == x' && y == y'
+
+-- Ex. 3
+data StringOrInt =
+ TisAnInt Int
+ | TisAString String
+
+instance eqStringOrInt :: Eq StringOrInt where
+ eq (TisAnInt x) (TisAnInt y) = x == y
+ eq (TisAString x) (TisAString y) = x == y
+ eq _ _ = false
+
+-- Ex. 4
+data Pair a =
+ Pair a a
+
+instance eqPair :: Eq a => Eq (Pair a) where
+ eq (Pair a b) (Pair a' b') = a == a' && b == b'
+
+-- Ex. 5
+data Tuple a b =
+ Tuple a b
+
+instance eqTuple :: (Eq a, Eq b) => Eq (Tuple a b) where
+ eq (Tuple a b) (Tuple a' b') = a == a' && b == b'
+
+-- Ex. 6
+data Which a =
+ ThisOne a
+ | ThatOne a
+
+instance eqWhich :: Eq a => Eq (Which a) where
+ eq (ThisOne x) (ThisOne y) = x == y
+ eq (ThatOne x) (ThatOne y) = x == y
+ eq _ _ = false
+
+-- Ex. 7
+data EitherOr a b =
+ Hello a
+ | Goodbye b
+
+instance eqEitherOr :: (Eq a, Eq b) => Eq (EitherOr a b) where
+ eq (Hello _) (Goodbye _) = false
+ eq (Goodbye _) (Hello _) = false
+ eq _ _ = true
diff --git a/Haskell-book/06/src/Ex.purs b/Haskell-book/06/src/Ex.purs
new file mode 100644
index 0000000..290ad8e
--- /dev/null
+++ b/Haskell-book/06/src/Ex.purs
@@ -0,0 +1,9 @@
+module Ex where
+
+import Prelude
+
+--
+-- Type-Kwon-Do Two: Electric Typealoo
+--
+chk :: forall a b. Eq b => (a -> b) -> a -> b -> Boolean
+chk f a b = (f a) == b
diff --git a/Haskell-book/06/src/Main.purs b/Haskell-book/06/src/Main.purs
new file mode 100644
index 0000000..6f60ab2
--- /dev/null
+++ b/Haskell-book/06/src/Main.purs
@@ -0,0 +1,30 @@
+module Main where
+
+import Prelude
+import Control.Monad.Eff (Eff)
+import Control.Monad.Eff.Console (CONSOLE, log)
+
+data DayOfWeek =
+ Mon | Tue | Weds | Thu | Fri | Sat | Sun
+
+data Date =
+ Date DayOfWeek Int
+
+instance eqDayOfWeek :: Eq DayOfWeek where
+ eq Mon Mon = true
+ eq Tue Tue = true
+ eq Weds Weds = true
+ eq Thu Thu = true
+ eq Fri Fri = true
+ eq Sat Sat = true
+ eq Sun Sun = true
+ eq _ _ = false
+
+instance eqDate :: Eq Date where
+ eq (Date weekday dayOfMonth)
+ (Date weekday' dayOfMonth') =
+ weekday == weekday' && dayOfMonth == dayOfMonth'
+
+main :: forall e. Eff (console :: CONSOLE | e) Unit
+main = do
+ log "Hello sailor!"
diff --git a/Haskell-book/06/test/Main.purs b/Haskell-book/06/test/Main.purs
new file mode 100644
index 0000000..845d0f4
--- /dev/null
+++ b/Haskell-book/06/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."