Skip to content

Commit 122e1e8

Browse files
pwadsworthpetertseng
authored andcommitted
Add ADTs concept
1 parent d32567a commit 122e1e8

File tree

6 files changed

+105
-0
lines changed

6 files changed

+105
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"authors": [
3+
"pwadsworth"
4+
],
5+
"blurb": "Introduction to Algebraic Data Types in Haskell."
6+
}
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Introduction
2+
3+
An Algebraic Data Type (ADT) represents a fixed number of named cases.
4+
Each value of an ADT corresponds to exactly one of the named cases.
5+
6+
An ADT is defined using the `data` keyword, with cases separated by pipe (`|`) characters.
7+
If none of the cases have data associated with them the ADT is similar to what other languages usually refer to as an _enumeration_ (or _enum_).
8+
9+
```haskell
10+
data Season
11+
= Spring
12+
| Summer
13+
| Autumn
14+
| Winter
15+
```
16+
17+
Each case of an ADT can optionally have data associated with it, and different cases can have different types of data. When the case has data associated, a constructor is required.
18+
19+
```haskell
20+
data Number
21+
= NInt Int --'NInt' is the constructor for an Int Number.
22+
| NFloat Float --'NFloat' is the constructor for an Float Number.
23+
| Invalid --'Invalid' does not have data associated to it.
24+
```
25+
26+
Creating a value for a specific case can be done by referring to its name (e.g, `NInt 22`).
27+
As case names are just constructor functions, associated data can be passed as a regular function argument.
28+
29+
ADTs have _structural equality_, which means that two values for the same case and with the same (optional) data are equivalent.
30+
31+
While one can use `if/else` expressions to work with ADTs, the recommended way to work with them is through pattern matching using _case_ statement:
32+
33+
```haskell
34+
add1 :: Number -> String
35+
add1 number =
36+
case number of
37+
NInt i -> show (i + 1)
38+
NFloat f -> show (f + 1.0)
39+
Invalid -> error "Invalid input"
40+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Introduction
2+
3+
An Algebraic Data Type (ADT) represents a fixed number of named cases.
4+
Each value of an ADT corresponds to exactly one of the named cases.
5+
6+
An ADT is defined using the `data` keyword, with cases separated by pipe (`|`) characters.
7+
If none of the cases have data associated with them the ADT is similar to what other languages usually refer to as an _enumeration_ (or _enum_).
8+
9+
```haskell
10+
data Season
11+
= Spring
12+
| Summer
13+
| Autumn
14+
| Winter
15+
```
16+
17+
Each case of an ADT can optionally have data associated with it, and different cases can have different types of data.
18+
When the case has data associated, a constructor is required.
19+
20+
```haskell
21+
data Number
22+
= NInt Int --'NInt' is the constructor for an Int Number.
23+
| NFloat Float --'NFloat' is the constructor for an Float Number.
24+
| Invalid --'Invalid' does not have data associated to it.
25+
```
26+
27+
Creating a value for a specific case can be done by referring to its name (e.g, `NInt 22`).
28+
As case names are just constructor functions, associated data can be passed as a regular function argument.
29+
30+
ADTs have _structural equality_, which means that two values for the same case and with the same (optional) data are equivalent.
31+
32+
While one can use `if/else` expressions to work with ADTs, the recommended way to work with them is through pattern matching using _case_ statement:
33+
34+
```haskell
35+
add1 :: Number -> String
36+
add1 number =
37+
case number of
38+
NInt i -> show (i + 1)
39+
NFloat f -> show (f + 1.0)
40+
Invalid -> error "Invalid input"
41+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Introduction
2+
3+
%{concept:algebraic-data-types}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{
3+
"url": "https://learnyouahaskell.github.io/making-our-own-types-and-typeclasses.html#algebraic-data-types",
4+
"description": "Algebraic Data Types"
5+
},
6+
{
7+
"url": "https://learnyouahaskell.github.io/making-our-own-types-and-typeclasses.html#type-parameters",
8+
"description": "Type variables"
9+
}
10+
]

config.json

+5
Original file line numberDiff line numberDiff line change
@@ -1267,6 +1267,11 @@
12671267
"uuid": "ca21a553-6fc1-49be-8f47-730f97330862",
12681268
"slug": "pattern-matching-literals",
12691269
"name": "Pattern Matching Literals"
1270+
},
1271+
{
1272+
"uuid": "cb756cfd-9b5c-408d-bf3f-b2adc8322da3",
1273+
"slug": "algebraic-data-types",
1274+
"name": "Algebraic Data Types"
12701275
}
12711276
],
12721277
"key_features": [

0 commit comments

Comments
 (0)