Skip to content

Commit

Permalink
More work
Browse files Browse the repository at this point in the history
  • Loading branch information
meatball133 committed Oct 10, 2024
1 parent e46aa84 commit 43dde8d
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 7 deletions.
6 changes: 6 additions & 0 deletions concepts/guards/.meta/config.json
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."
}
30 changes: 30 additions & 0 deletions concepts/guards/about.md
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"
```
30 changes: 30 additions & 0 deletions concepts/guards/introduction.md
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"
```
1 change: 1 addition & 0 deletions concepts/guards/links.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
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"

0 comments on commit 43dde8d

Please sign in to comment.