-
-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
744d7b1
commit 351b322
Showing
20 changed files
with
633 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"authors": [ | ||
"pwadsworth" | ||
"meatball133" | ||
], | ||
"blurb": "Introduction to pattern matching in Haskell." | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,89 @@ | ||
# About | ||
|
||
When writing Haskell functions, we can make use of an [assertive style][assertive-style] with [pattern matching][pattern-match-doc]: | ||
When writing Haskell functions, we can make use of [pattern matching][pattern-matching]. | ||
Pattern matching is a very powerful feature of Haskell that allows you to match on data constructors and bind variables to the values inside. | ||
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. | ||
Pattern matching is mainly built of these concepts: recognizing values, binding variables, and breaking down values. | ||
|
||
~~~~exercism/note | ||
Pattern matching in languages such as Elixir is a bit different from pattern matching in Haskell. | ||
Elixir for example allows multiple of same binding names in a pattern, while Haskell does not. | ||
~~~~ | ||
|
||
|
||
Take this function for example: | ||
|
||
```haskell | ||
lucky :: Int -> String | ||
lucky 7 = "Lucky number seven!" | ||
lucky x = "Sorry, you're out of luck, pal!" | ||
``` | ||
|
||
Here we have a function `lucky` that takes an `Int` and returns a `String`. | ||
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. | ||
If the number is `7`, the function will return `"Lucky number seven!"`, otherwise it will return `"Sorry, you're out of luck, pal!"`. | ||
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!"`. | ||
|
||
## List patterns | ||
|
||
A very common pattern is to match on lists, and that is taking the head and the tail of the list. | ||
This is due to lists nature of being a linked list. | ||
Here is an example of a function that returns the head and the tail of a list: | ||
|
||
```haskell | ||
headAndTail :: [Int] -> (Int, [Int]) | ||
headAndTail [] = error "Can't call head on an empty list" | ||
headAndTail (x:xs) = (x, xs) | ||
``` | ||
|
||
We have two patterns here, one that matches an empty list and one that matches a list with at least one element. | ||
This is due to if the list is empty, we need to have a case for that, otherwise we would get a runtime error. | ||
If the list is not empty, we can match the head of the list with `x` and the tail of the list with `xs`. | ||
This is done using the `:` (cons) operator, which is used to prepend an element to a list. | ||
But in pattern matching it allows us to break down a list into its head and tail, so in a way doing the opposite. | ||
|
||
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. | ||
|
||
## Tuple patterns | ||
|
||
As with lists, we can also match on tuples. | ||
Here is an example of a function that takes a tuple and returns the first and second element: | ||
|
||
```haskell | ||
read_file = do | ||
{:ok, contents} <- readFile "hello.txt" | ||
contents | ||
sumTuple :: (Int, Int) -> Int | ||
sumTuple (x, y) = x + y | ||
``` | ||
|
||
- Pattern matching is explicitly performed using the match operator, [`=/2`][match-op]. | ||
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`. | ||
|
||
## Wildcard patterns | ||
|
||
Sometimes we don't care about the value of a variable, we just want to match on the pattern. | ||
This is where the wildcard pattern comes in, it is denoted by an underscore `_`. | ||
|
||
- Matches succeed when the _shape_ of the data on the left side of the operator matches the right side. | ||
- When matches succeed, variables on the left are bound to the values on the right. | ||
- Using an underscore, `_` (also know as wildcard), allows us to disregard the values in those places. | ||
Here is an example of a function that returns the first element of a list: | ||
|
||
```haskell | ||
(True, number, _) <- (True, 5, [4.5, 6.3]) | ||
number | ||
-- -> 5 is bound to this variable | ||
``` | ||
```haskell | ||
head' :: [Int] -> Int | ||
head' [] = error "Can't call head on an empty list" | ||
head' (x:_) = x | ||
``` | ||
|
||
Here we say we don't need the tail of the list, so we use the wildcard pattern to ignore it. | ||
|
||
- Pattern matches may also occur in a function clause head, so that only arguments that match the pattern will invoke the function. | ||
- Variables can be bound in a function clause pattern match. | ||
## Type patterns | ||
|
||
```haskell | ||
named_function :: Bool -> (Bool, Int) | ||
named_function True = (True, 1) | ||
We can also match on types, this is done by using the constructor of said type. | ||
We can also extract values from the type, like we did with tuples. | ||
|
||
named_function(True) | ||
-- -> (True, 1) | ||
-- The first function clause matches, so it is invoked | ||
```haskell | ||
data AccountType = Guest | User String | ||
|
||
greet :: AccountType -> String | ||
greet Guest = "Welcome, guest!" | ||
greet (User name) = "Welcome, " ++ name ++ "!" | ||
``` | ||
|
||
named_function(False) | ||
-- (FunctionClauseError) Non-exhaustive patterns in function named_function | ||
``` | ||
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`. | ||
|
||
[assertive-style]: https://blog.plataformatec.com.br/2014/09/writing-assertive-code-with-elixir/ | ||
[pattern-match-doc]: https://hexdocs.pm/elixir/pattern-matching.html | ||
[match-op]: https://hexdocs.pm/elixir/Kernel.SpecialForms.html#=/2 | ||
[getting-started-pin-operator]: https://hexdocs.pm/elixir/pattern-matching.html#the-pin-operator | ||
[pattern-matching]: https://en.wikibooks.org/wiki/Haskell/Pattern_matching |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,89 @@ | ||
# Introduction | ||
# About | ||
|
||
While `if/else` expressions can be used to execute conditional logic, Haskell also has a more powerful way to execute conditional logic: [pattern matching][pattern-matching]. | ||
With pattern matching, a value can be tested against one or more _patterns_. | ||
An example of such a pattern is the _constant pattern_, which matches a value against a constant (e.g. `1` or `"hello"`). | ||
When writing Haskell functions, we can make use of [pattern matching][pattern-matching]. | ||
Pattern matching is a very powerful feature of Haskell that allows you to match on data constructors and bind variables to the values inside. | ||
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. | ||
Pattern matching is mainly built of these concepts: recognizing values, binding variables, and breaking down values. | ||
|
||
When defining functions, you can define separate function bodies for different patterns. | ||
This leads to clean code that is simple and readable. | ||
You can pattern match on any data type — numbers, characters, lists, tuples, etc. | ||
~~~~exercism/note | ||
Pattern matching in languages such as Elixir is a bit different from pattern matching in Haskell. | ||
Elixir for example allows multiple of same binding names in a pattern, while Haskell does not. | ||
~~~~ | ||
|
||
For example, a trivial function that takes a whole number (`Int`) and makes it _1_ closer to _0_ could be expressed like this: | ||
|
||
Take this function for example: | ||
|
||
```haskell | ||
lucky :: Int -> String | ||
lucky 7 = "Lucky number seven!" | ||
lucky x = "Sorry, you're out of luck, pal!" | ||
``` | ||
|
||
Here we have a function `lucky` that takes an `Int` and returns a `String`. | ||
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. | ||
If the number is `7`, the function will return `"Lucky number seven!"`, otherwise it will return `"Sorry, you're out of luck, pal!"`. | ||
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!"`. | ||
|
||
## List patterns | ||
|
||
A very common pattern is to match on lists, and that is taking the head and the tail of the list. | ||
This is due to lists nature of being a linked list. | ||
Here is an example of a function that returns the head and the tail of a list: | ||
|
||
```haskell | ||
closerToZero :: Int -> Int | ||
closerToZero 0 = 0 | ||
closerToZero 1 = 0 | ||
headAndTail :: [Int] -> (Int, [Int]) | ||
headAndTail [] = error "Can't call head on an empty list" | ||
headAndTail (x:xs) = (x, xs) | ||
``` | ||
|
||
Pattern matching starts to shine when used together with other patterns, for example the _variable pattern_: | ||
We have two patterns here, one that matches an empty list and one that matches a list with at least one element. | ||
This is due to if the list is empty, we need to have a case for that, otherwise we would get a runtime error. | ||
If the list is not empty, we can match the head of the list with `x` and the tail of the list with `xs`. | ||
This is done using the `:` (cons) operator, which is used to prepend an element to a list. | ||
But in pattern matching it allows us to break down a list into its head and tail, so in a way doing the opposite. | ||
|
||
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. | ||
|
||
## Tuple patterns | ||
|
||
As with lists, we can also match on tuples. | ||
Here is an example of a function that takes a tuple and returns the first and second element: | ||
|
||
```haskell | ||
closerToZero :: Int -> Int | ||
closerToZero 0 = 0 | ||
closerToZero n = n - 1 | ||
sumTuple :: (Int, Int) -> Int | ||
sumTuple (x, y) = x + y | ||
``` | ||
|
||
The above example treats all inputs other than _0_ the same, and would produce incorrect results for negative numbers. | ||
This can be solved using conditional patterns, known as _guards_, which are expressed with the `|` symbol: | ||
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`. | ||
|
||
## Wildcard patterns | ||
|
||
Sometimes we don't care about the value of a variable, we just want to match on the pattern. | ||
This is where the wildcard pattern comes in, it is denoted by an underscore `_`. | ||
|
||
Here is an example of a function that returns the first element of a list: | ||
|
||
```haskell | ||
closerToZero :: Int -> Int | ||
closerToZero n | ||
| n < 0 = n + 1 | ||
| n > 0 = n - 1 | ||
head' :: [Int] -> Int | ||
head' [] = error "Can't call head on an empty list" | ||
head' (x:_) = x | ||
``` | ||
|
||
In the above examples not all possible inputs have a matching pattern. | ||
The compiler will detect this and output a warning. | ||
This is a very useful feature of Haskell that helps ensure all possible paths are covered to avoid run-time errors. | ||
It is known as _exhaustive checking_. | ||
To solve the warning, you have to handle all cases. | ||
Within _guards_ you can use the expression `otherwise` as syntactic sugar for `True` to catch all remaining patterns. | ||
Here we say we don't need the tail of the list, so we use the wildcard pattern to ignore it. | ||
|
||
## Type patterns | ||
|
||
We can also match on types, this is done by using the constructor of said type. | ||
We can also extract values from the type, like we did with tuples. | ||
|
||
```haskell | ||
closerToZero :: Int -> Int | ||
closerToZero n | ||
| n < 0 = n + 1 | ||
| n > 0 = n - 1 | ||
| otherwise = 0 | ||
data AccountType = Guest | User String | ||
|
||
greet :: AccountType -> String | ||
greet Guest = "Welcome, guest!" | ||
greet (User name) = "Welcome, " ++ name ++ "!" | ||
``` | ||
|
||
Pattern matching will test a value against each pattern from top to bottom, until it finds a matching pattern and executes the logic associated with that pattern. | ||
**The order of patterns matters!** | ||
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`. | ||
|
||
[pattern-matching]: https://learnyouahaskell.github.io/syntax-in-functions#pattern-matching | ||
[pattern-matching]: https://en.wikibooks.org/wiki/Haskell/Pattern_matching |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,6 @@ | ||
[ | ||
{ | ||
"url": "https://learnyouahaskell.github.io/syntax-in-functions", | ||
"description": "Pattern matching syntax in functions" | ||
}, | ||
{ | ||
"url": "https://learnyouahaskell.github.io/syntax-in-functions#guards-guards", | ||
"description": "Use of guards for pattern matching" | ||
"url": "https://en.wikibooks.org/wiki/Haskell/Pattern_matching", | ||
"description": "Wikibooks: matching" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"authors": [ | ||
"meatball133" | ||
], | ||
"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." | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# About | ||
|
||
[Tuples][tuple] are used commonly to group information, they differ from lists in that they are fixed-size and can hold different data types. | ||
Tuples are created using curly braces, `()`, and are often used to return multiple values from a function. | ||
|
||
```haskell | ||
tuple = (1, "abc", False) | ||
``` | ||
|
||
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. | ||
|
||
```haskell | ||
coordinate = (3, 4) | ||
``` | ||
|
||
## Accessing elements | ||
|
||
Quite often you work with short tuples, and you can access the elements using the `fst` and `snd` functions. | ||
|
||
```haskell | ||
x = fst coordinate | ||
-- x = 3 | ||
y = snd coordinate | ||
-- y = 4 | ||
``` | ||
|
||
[tuple]: https://hackage.haskell.org/package/base/docs/Data-Tuple.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# About | ||
|
||
[Tuples][tuple] are used commonly to group information, they differ from lists in that they are fixed-size and can hold different data types. | ||
Tuples are created using curly braces, `()`, and are often used to return multiple values from a function. | ||
|
||
```haskell | ||
tuple = (1, "abc", False) | ||
``` | ||
|
||
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. | ||
|
||
```haskell | ||
coordinate = (3, 4) | ||
``` | ||
|
||
## Accessing elements | ||
|
||
Quite often you work with short tuples, and you can access the elements using the `fst` and `snd` functions. | ||
|
||
```haskell | ||
x = fst coordinate | ||
-- x = 3 | ||
y = snd coordinate | ||
-- y = 4 | ||
``` | ||
|
||
[tuple]: https://hackage.haskell.org/package/base/docs/Data-Tuple.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[ | ||
{ | ||
"url": "https://en.wikibooks.org/wiki/Haskell/Lists_and_tuples#Tuples", | ||
"description": "Wikibooks: Haskell/Lists and tuples" | ||
}, | ||
{ | ||
"url": "https://hackage.haskell.org/package/base/docs/Data-Tuple.html", | ||
"description": "Hackae: Data.Tuple" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.