Skip to content

Commit ab58774

Browse files
committed
Add ADTs concept
1 parent c645312 commit ab58774

File tree

6 files changed

+99
-1
lines changed

6 files changed

+99
-1
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+
}
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Introduction
2+
3+
An Algebraic Data Type (ADT) represents a fixed number of named cases. Each value of an ADT corresponds to exactly one of the named cases.
4+
5+
An ADT is defined using the `data` keyword, with cases separated by pipe (`|`) characters. 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_).
6+
7+
```haskell
8+
data Season
9+
= Spring
10+
| Summer
11+
| Autumn
12+
| Winter
13+
```
14+
15+
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.
16+
17+
```haskell
18+
data Number
19+
= NInt Int --'NInt' is the constructor for an Int Number.
20+
| NFloat Float --'NFloat' is the constructor for an Float Number.
21+
| Invalid --'Invalid' does not have data associated to it.
22+
```
23+
24+
Creating a value for a specific case can be done by referring to its name (e.g, `NInt 22`). As case names are just constructor functions, associated data can be passed as a regular function argument.
25+
26+
ADTs have _structural equality_, which means that two values for the same case and with the same (optional) data are equivalent.
27+
28+
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:
29+
30+
```haskell
31+
add1 :: Number -> String
32+
add1 number =
33+
case number of
34+
NInt i -> show (i + 1)
35+
NFloat f -> show (f + 1.0)
36+
Invalid -> error "Invalid input"
37+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Introduction
2+
3+
An Algebraic Data Type (ADT) represents a fixed number of named cases. Each value of an ADT corresponds to exactly one of the named cases.
4+
5+
An ADT is defined using the `data` keyword, with cases separated by pipe (`|`) characters. 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_).
6+
7+
```haskell
8+
data Season
9+
= Spring
10+
| Summer
11+
| Autumn
12+
| Winter
13+
```
14+
15+
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.
16+
17+
```haskell
18+
data Number
19+
= NInt Int --'NInt' is the constructor for an Int Number.
20+
| NFloat Float --'NFloat' is the constructor for an Float Number.
21+
| Invalid --'Invalid' does not have data associated to it.
22+
```
23+
24+
Creating a value for a specific case can be done by referring to its name (e.g, `NInt 22`). As case names are just constructor functions, associated data can be passed as a regular function argument.
25+
26+
ADTs have _structural equality_, which means that two values for the same case and with the same (optional) data are equivalent.
27+
28+
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:
29+
30+
```haskell
31+
add1 :: Number -> String
32+
add1 number =
33+
case number of
34+
NInt i -> show (i + 1)
35+
NFloat f -> show (f + 1.0)
36+
Invalid -> error "Invalid input"
37+
```
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

+6-1
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,11 @@
12641264
"uuid": "ca21a553-6fc1-49be-8f47-730f97330862",
12651265
"slug": "pattern-matching-literals",
12661266
"name": "Pattern Matching Literals"
1267+
},
1268+
{
1269+
"uuid": "cb756cfd-9b5c-408d-bf3f-b2adc8322da3",
1270+
"slug": "algebraic-data-types",
1271+
"name": "Algebraic Data Types"
12671272
}
12681273
],
12691274
"key_features": [
@@ -1301,4 +1306,4 @@
13011306
"tags": [
13021307
"paradigm/functional"
13031308
]
1304-
}
1309+
}

0 commit comments

Comments
 (0)