summaryrefslogtreecommitdiff
path: root/lec02
diff options
context:
space:
mode:
authorEdoardo La Greca2025-06-21 15:55:29 +0200
committerEdoardo La Greca2025-06-21 21:25:00 +0200
commit28291764fa543497198ed737b109c80c79c51b2c (patch)
tree033663050eec125715cf0e4f8ebbd0c9753b8ade /lec02
parent5be14fbbb9561d17099cd2972c2ca5437b9343f6 (diff)
add lecture 2 notesHEADmaster
Diffstat (limited to 'lec02')
-rw-r--r--lec02/notes.md38
1 files changed, 38 insertions, 0 deletions
diff --git a/lec02/notes.md b/lec02/notes.md
new file mode 100644
index 0000000..7420f92
--- /dev/null
+++ b/lec02/notes.md
@@ -0,0 +1,38 @@
+# Lecture 2: Algebraic data types
+
+## Enumeration types
+
+ data Thing = Shoe
+ | Ship
+ | SealingWax
+ | Cabbage
+ | King
+ derivating Show
+
+The example above is a **type declaration**. The new type is called `Thing` and it has 5 **data constructors** (`Shoe`, `Ship`, etc.). A type's constructors are the only values for that type.
+
+The line that says `deriving Show` is a special instruction that tells the GHC to automatically generate default code for converting a `Thing` to `String`.
+
+ Shoe :: Thing
+ Shoe = Shoe
+
+ listO'Things :: [Thing]
+ listO'Things = [Show, SealingWax, King, Cabbage, King]
+
+Functions on enumeration types can be written using pattern-matching.
+
+ isSmall :: Thing -> Bool
+ isSmall Show = True
+ isSmall Ship = False
+ isSmall SealingWax = True
+ isSmall Cabbage = True
+ isSmall King = False
+
+Since function clauses are checked from top to bottom, `isSmall` can be rewritten like this:
+
+ isSmall2 :: Thing -> Bool
+ isSmall2 Ship = False
+ isSmall2 King = False
+ isSmall2 _ = True
+
+## Beyond enumeration