-
-
Notifications
You must be signed in to change notification settings - Fork 195
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
e46aa84
commit 43dde8d
Showing
5 changed files
with
73 additions
and
7 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"authors": [ | ||
"meatball133" | ||
], | ||
"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." | ||
} |
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,30 @@ | ||
# About | ||
|
||
[Guards][guards] are used as a complement to [pattern matching][exercism-pattern-matching]. | ||
Which we will cover in a later concept. | ||
Guards allows us to have different implementations of a function depending on the value of the input. | ||
They allow for more complex checks. | ||
|
||
A guard statement is defined by a pipe `|` followed by a boolean expression, ending with an equal sign `=` and the functions body. | ||
There can be multiple guard statements for a single function. | ||
The guard statements is evaluated from top to bottom, and the first one that evaluates to `True` will be executed. | ||
A guard statement allows for a `otherwise` statement, which is a catch-all for any value that doesn't match the previous guard statements. | ||
|
||
```haskell | ||
isEven :: Int -> String | ||
isEven n | ||
| even n = "n is even" | ||
| otherwise = "n is odd" | ||
``` | ||
|
||
We can also deffine our function and use it inside the guard statement. | ||
|
||
```haskell | ||
isEven' :: Int -> Bool | ||
isEven' n = even n | ||
|
||
isEven :: Int -> String | ||
isEven n | ||
| isEven' n = "n is even" | ||
| otherwise = "n is odd" | ||
``` |
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,30 @@ | ||
# About | ||
|
||
[Guards][guards] are used as a complement to [pattern matching][exercism-pattern-matching]. | ||
Which we will cover in a later concept. | ||
Guards allows us to have different implementations of a function depending on the value of the input. | ||
They allow for more complex checks. | ||
|
||
A guard statement is defined by a pipe `|` followed by a boolean expression, ending with an equal sign `=` and the functions body. | ||
There can be multiple guard statements for a single function. | ||
The guard statements is evaluated from top to bottom, and the first one that evaluates to `True` will be executed. | ||
A guard statement allows for a `otherwise` statement, which is a catch-all for any value that doesn't match the previous guard statements. | ||
|
||
```haskell | ||
isEven :: Int -> String | ||
isEven n | ||
| even n = "n is even" | ||
| otherwise = "n is odd" | ||
``` | ||
|
||
We can also deffine our function and use it inside the guard statement. | ||
|
||
```haskell | ||
isEven' :: Int -> Bool | ||
isEven' n = even n | ||
|
||
isEven :: Int -> String | ||
isEven n | ||
| isEven' n = "n is even" | ||
| otherwise = "n is odd" | ||
``` |
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 @@ | ||
[] |
13 changes: 6 additions & 7 deletions
13
exercises/concept/guessing-game/.meta/exemplar/src/GuessingGame.hs
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,9 +1,8 @@ | ||
module GuessingGame (reply) where | ||
|
||
reply :: Int -> String | ||
reply 41 = "So close" | ||
reply 42 = "Correct" | ||
reply 43 = "So close" | ||
reply guess | ||
| guess < 41 = "Too low" | ||
| otherwise = "Too high" | ||
reply :: Int -> Int -> String | ||
reply n guess | ||
| guess == n = "Correct" | ||
| guess + 1 == n || guess - 1 == n = "So close!" | ||
| guess < n = "Too low" | ||
| guess > n = "Too high" |