From 43dde8dae34933dee1b4ebe3e001d54e8c02c189 Mon Sep 17 00:00:00 2001 From: meatball Date: Thu, 10 Oct 2024 22:14:49 +0200 Subject: [PATCH] More work --- concepts/guards/.meta/config.json | 6 ++++ concepts/guards/about.md | 30 +++++++++++++++++++ concepts/guards/introduction.md | 30 +++++++++++++++++++ concepts/guards/links.json | 1 + .../.meta/exemplar/src/GuessingGame.hs | 13 ++++---- 5 files changed, 73 insertions(+), 7 deletions(-) create mode 100644 concepts/guards/.meta/config.json create mode 100644 concepts/guards/about.md create mode 100644 concepts/guards/introduction.md create mode 100644 concepts/guards/links.json diff --git a/concepts/guards/.meta/config.json b/concepts/guards/.meta/config.json new file mode 100644 index 000000000..7ffe8f6c1 --- /dev/null +++ b/concepts/guards/.meta/config.json @@ -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." +} diff --git a/concepts/guards/about.md b/concepts/guards/about.md new file mode 100644 index 000000000..5893b3a54 --- /dev/null +++ b/concepts/guards/about.md @@ -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" +``` diff --git a/concepts/guards/introduction.md b/concepts/guards/introduction.md new file mode 100644 index 000000000..5893b3a54 --- /dev/null +++ b/concepts/guards/introduction.md @@ -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" +``` diff --git a/concepts/guards/links.json b/concepts/guards/links.json new file mode 100644 index 000000000..fe51488c7 --- /dev/null +++ b/concepts/guards/links.json @@ -0,0 +1 @@ +[] diff --git a/exercises/concept/guessing-game/.meta/exemplar/src/GuessingGame.hs b/exercises/concept/guessing-game/.meta/exemplar/src/GuessingGame.hs index 9f0c96d05..b111a9e23 100644 --- a/exercises/concept/guessing-game/.meta/exemplar/src/GuessingGame.hs +++ b/exercises/concept/guessing-game/.meta/exemplar/src/GuessingGame.hs @@ -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"