Skip to content

Commit 5910ff5

Browse files
authored
Add pattern matching and tuple concept (#1275)
* Start * Add concept and exercise * Rename folder * Rename tuple concept * More work
1 parent 4c4156a commit 5910ff5

25 files changed

Lines changed: 748 additions & 7 deletions

File tree

concepts/guards/.meta/config.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"authors": [
3+
"meatball133"
4+
],
5+
"blurb": "Guards are used to prevent function invocation based on evaluation of the arguments by guard functions. Guards begin with the `|` operatpr, followed by a boolean expression. Guards are used to augment pattern matching with more complex checks."
6+
}

concepts/guards/about.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# About
2+
3+
[Guards][guards] are used as a complement to [pattern matching][exercism-pattern-matching].
4+
Which we will cover in a later concept.
5+
Guards allows us to have different implementations of a function depending on the value of the input.
6+
They allow for more complex checks.
7+
8+
A guard statement is defined by a pipe `|` followed by a boolean expression, ending with an equal sign `=` and the functions body.
9+
There can be multiple guard statements for a single function.
10+
The guard statements is evaluated from top to bottom, and the first one that evaluates to `True` will be executed.
11+
A guard statement allows for a `otherwise` statement, which is a catch-all for any value that doesn't match the previous guard statements.
12+
13+
```haskell
14+
isEven :: Int -> String
15+
isEven n
16+
| even n = "n is even"
17+
| otherwise = "n is odd"
18+
```
19+
20+
We can also deffine our function and use it inside the guard statement.
21+
22+
```haskell
23+
isEven' :: Int -> Bool
24+
isEven' n = even n
25+
26+
isEven :: Int -> String
27+
isEven n
28+
| isEven' n = "n is even"
29+
| otherwise = "n is odd"
30+
```

concepts/guards/introduction.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# About
2+
3+
[Guards][guards] are used as a complement to [pattern matching][exercism-pattern-matching].
4+
Which we will cover in a later concept.
5+
Guards allows us to have different implementations of a function depending on the value of the input.
6+
They allow for more complex checks.
7+
8+
A guard statement is defined by a pipe `|` followed by a boolean expression, ending with an equal sign `=` and the functions body.
9+
There can be multiple guard statements for a single function.
10+
The guard statements is evaluated from top to bottom, and the first one that evaluates to `True` will be executed.
11+
A guard statement allows for a `otherwise` statement, which is a catch-all for any value that doesn't match the previous guard statements.
12+
13+
```haskell
14+
isEven :: Int -> String
15+
isEven n
16+
| even n = "n is even"
17+
| otherwise = "n is odd"
18+
```
19+
20+
We can also deffine our function and use it inside the guard statement.
21+
22+
```haskell
23+
isEven' :: Int -> Bool
24+
isEven' n = even n
25+
26+
isEven :: Int -> String
27+
isEven n
28+
| isEven' n = "n is even"
29+
| otherwise = "n is odd"
30+
```

concepts/guards/links.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"authors": [
3+
"meatball133"
4+
],
5+
"blurb": "Introduction to pattern matching in Haskell."
6+
}

concepts/pattern-matching/about.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# About
2+
3+
When writing Haskell functions, we can make use of [pattern matching][pattern-matching].
4+
Pattern matching is a very powerful feature of Haskell that allows you to match on data constructors and bind variables to the values inside.
5+
As the name suggests, what we do is match values against patterns, and if the value matches the pattern, we can bind variables to the values inside the pattern.
6+
Pattern matching is mainly built of these concepts: recognizing values, binding variables, and breaking down values.
7+
8+
~~~~exercism/note
9+
Pattern matching in languages such as Elixir is a bit different from pattern matching in Haskell.
10+
Elixir for example allows multiple of same binding names in a pattern, while Haskell does not.
11+
~~~~
12+
13+
14+
Take this function for example:
15+
16+
```haskell
17+
lucky :: Int -> String
18+
lucky 7 = "Lucky number seven!"
19+
lucky x = "Sorry, you're out of luck, pal!"
20+
```
21+
22+
Here we have a function `lucky` that takes an `Int` and returns a `String`.
23+
We have defined two patterns for the function, one that matches the number `7` and one that matches any other number, the name can be anything (as long as it follows Haskell naming convention), but we use `x` here.
24+
If the number is `7`, the function will return `"Lucky number seven!"`, otherwise it will return `"Sorry, you're out of luck, pal!"`.
25+
What is important to note here is that the patterns are checked from top to bottom, so if we had swapped the order of the patterns, the function would always return `"Lucky number seven!"`.
26+
27+
## List patterns
28+
29+
A very common pattern is to match on lists, and that is taking the head and the tail of the list.
30+
This is due to lists nature of being a linked list.
31+
Here is an example of a function that returns the head and the tail of a list:
32+
33+
```haskell
34+
headAndTail :: [Int] -> (Int, [Int])
35+
headAndTail [] = error "Can't call head on an empty list"
36+
headAndTail (x:xs) = (x, xs)
37+
```
38+
39+
We have two patterns here, one that matches an empty list and one that matches a list with at least one element.
40+
This is due to if the list is empty, we need to have a case for that, otherwise we would get a runtime error.
41+
If the list is not empty, we can match the head of the list with `x` and the tail of the list with `xs`.
42+
This is done using the `:` (cons) operator, which is used to prepend an element to a list.
43+
But in pattern matching it allows us to break down a list into its head and tail, so in a way doing the opposite.
44+
45+
The `xs` is a common name for the tail of a list, it highlights that it is a list, but if you would be working with a nested list, you could use `xss` to highlight that it is a list of lists.
46+
47+
## Tuple patterns
48+
49+
As with lists, we can also match on tuples.
50+
Here is an example of a function that takes a tuple and returns the first and second element:
51+
52+
```haskell
53+
sumTuple :: (Int, Int) -> Int
54+
sumTuple (x, y) = x + y
55+
```
56+
57+
Here we have a pattern that matches a tuple with two elements, the first element is bound to `x` and the second element is bound to `y`.
58+
59+
## Wildcard patterns
60+
61+
Sometimes we don't care about the value of a variable, we just want to match on the pattern.
62+
This is where the wildcard pattern comes in, it is denoted by an underscore `_`.
63+
64+
Here is an example of a function that returns the first element of a list:
65+
66+
```haskell
67+
head' :: [Int] -> Int
68+
head' [] = error "Can't call head on an empty list"
69+
head' (x:_) = x
70+
```
71+
72+
Here we say we don't need the tail of the list, so we use the wildcard pattern to ignore it.
73+
74+
## Type patterns
75+
76+
We can also match on types, this is done by using the constructor of said type.
77+
We can also extract values from the type, like we did with tuples.
78+
79+
```haskell
80+
data AccountType = Guest | User String
81+
82+
greet :: AccountType -> String
83+
greet Guest = "Welcome, guest!"
84+
greet (User name) = "Welcome, " ++ name ++ "!"
85+
```
86+
87+
In the first pattern we match on the `Guest` constructor, and in the second pattern we match on the `User` constructor and bind the value inside to `name`.
88+
89+
[pattern-matching]: https://en.wikibooks.org/wiki/Haskell/Pattern_matching
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# About
2+
3+
When writing Haskell functions, we can make use of [pattern matching][pattern-matching].
4+
Pattern matching is a very powerful feature of Haskell that allows you to match on data constructors and bind variables to the values inside.
5+
As the name suggests, what we do is match values against patterns, and if the value matches the pattern, we can bind variables to the values inside the pattern.
6+
Pattern matching is mainly built of these concepts: recognizing values, binding variables, and breaking down values.
7+
8+
~~~~exercism/note
9+
Pattern matching in languages such as Elixir is a bit different from pattern matching in Haskell.
10+
Elixir for example allows multiple of same binding names in a pattern, while Haskell does not.
11+
~~~~
12+
13+
14+
Take this function for example:
15+
16+
```haskell
17+
lucky :: Int -> String
18+
lucky 7 = "Lucky number seven!"
19+
lucky x = "Sorry, you're out of luck, pal!"
20+
```
21+
22+
Here we have a function `lucky` that takes an `Int` and returns a `String`.
23+
We have defined two patterns for the function, one that matches the number `7` and one that matches any other number, the name can be anything (as long as it follows Haskell naming convention), but we use `x` here.
24+
If the number is `7`, the function will return `"Lucky number seven!"`, otherwise it will return `"Sorry, you're out of luck, pal!"`.
25+
What is important to note here is that the patterns are checked from top to bottom, so if we had swapped the order of the patterns, the function would always return `"Lucky number seven!"`.
26+
27+
## List patterns
28+
29+
A very common pattern is to match on lists, and that is taking the head and the tail of the list.
30+
This is due to lists nature of being a linked list.
31+
Here is an example of a function that returns the head and the tail of a list:
32+
33+
```haskell
34+
headAndTail :: [Int] -> (Int, [Int])
35+
headAndTail [] = error "Can't call head on an empty list"
36+
headAndTail (x:xs) = (x, xs)
37+
```
38+
39+
We have two patterns here, one that matches an empty list and one that matches a list with at least one element.
40+
This is due to if the list is empty, we need to have a case for that, otherwise we would get a runtime error.
41+
If the list is not empty, we can match the head of the list with `x` and the tail of the list with `xs`.
42+
This is done using the `:` (cons) operator, which is used to prepend an element to a list.
43+
But in pattern matching it allows us to break down a list into its head and tail, so in a way doing the opposite.
44+
45+
The `xs` is a common name for the tail of a list, it highlights that it is a list, but if you would be working with a nested list, you could use `xss` to highlight that it is a list of lists.
46+
47+
## Tuple patterns
48+
49+
As with lists, we can also match on tuples.
50+
Here is an example of a function that takes a tuple and returns the first and second element:
51+
52+
```haskell
53+
sumTuple :: (Int, Int) -> Int
54+
sumTuple (x, y) = x + y
55+
```
56+
57+
Here we have a pattern that matches a tuple with two elements, the first element is bound to `x` and the second element is bound to `y`.
58+
59+
## Wildcard patterns
60+
61+
Sometimes we don't care about the value of a variable, we just want to match on the pattern.
62+
This is where the wildcard pattern comes in, it is denoted by an underscore `_`.
63+
64+
Here is an example of a function that returns the first element of a list:
65+
66+
```haskell
67+
head' :: [Int] -> Int
68+
head' [] = error "Can't call head on an empty list"
69+
head' (x:_) = x
70+
```
71+
72+
Here we say we don't need the tail of the list, so we use the wildcard pattern to ignore it.
73+
74+
## Type patterns
75+
76+
We can also match on types, this is done by using the constructor of said type.
77+
We can also extract values from the type, like we did with tuples.
78+
79+
```haskell
80+
data AccountType = Guest | User String
81+
82+
greet :: AccountType -> String
83+
greet Guest = "Welcome, guest!"
84+
greet (User name) = "Welcome, " ++ name ++ "!"
85+
```
86+
87+
In the first pattern we match on the `Guest` constructor, and in the second pattern we match on the `User` constructor and bind the value inside to `name`.
88+
89+
[pattern-matching]: https://en.wikibooks.org/wiki/Haskell/Pattern_matching
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
{
3+
"url": "https://en.wikibooks.org/wiki/Haskell/Pattern_matching",
4+
"description": "Wikibooks: matching"
5+
}
6+
]

concepts/tuples/.meta/config.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"authors": [
3+
"meatball133"
4+
],
5+
"blurb": "A tuple is a data structure which organizes data, holding a fixed number of items of any type, but without explicit names for each element. Tuples are ideal for storing related information together, but not for storing collections of items that need iterating or might grow or shrink."
6+
}

concepts/tuples/about.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# About
2+
3+
[Tuples][tuple] are used commonly to group information, they differ from lists in that they are fixed-size and can hold different data types.
4+
Tuples are created using curly braces, `()`, and are often used to return multiple values from a function.
5+
6+
```haskell
7+
tuple = (1, "abc", False)
8+
```
9+
10+
This can be useful when you for example want to store a coordinate in a 2D space, then you know that the tuple will always have two elements, the x and y coordinate.
11+
12+
```haskell
13+
coordinate = (3, 4)
14+
```
15+
16+
## Accessing elements
17+
18+
Quite often you work with short tuples, and you can access the elements using the `fst` and `snd` functions.
19+
20+
```haskell
21+
x = fst coordinate
22+
-- x = 3
23+
y = snd coordinate
24+
-- y = 4
25+
```
26+
27+
[tuple]: https://hackage.haskell.org/package/base/docs/Data-Tuple.html

0 commit comments

Comments
 (0)