Skip to content

Commit

Permalink
make usingSafeDivide return a String
Browse files Browse the repository at this point in the history
  • Loading branch information
GoNZooo committed May 18, 2021
1 parent 0c0d32d commit 81cdd0b
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions basics/01-values-and-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- [Exercises (Functions)](#exercises-functions)
- [Exercise notes (Functions)](#exercise-notes-functions)
- [Asking questions about values](#asking-questions-about-values)
- [`if` expressions](#if-expressions)
- [Guards](#guards)
- [Multi-way `if`](#multi-way-if)
- [`case` expressions](#case-expressions)
Expand Down Expand Up @@ -410,14 +411,16 @@ We can use `case` to immediately ask questions about this structure:
import qualified System.Environment as Environment
import Prelude

usingSafeDivide :: IO ()
usingSafeDivide :: String
usingSafeDivide x divisor =
-- Note how we use `case` here to deconstruct the result
case safeDivide x divisor of
DivideSuccess result ->
putStrLn $ "Your result was: " <> show result
-- `show` takes a "showable" value and turns it into a `String`
-- `<>` here is a way to concatenate two strings together
"Your result was: " <> show result
DivisionByZero ->
putStrLn "You tried to divide by zero"
"You tried to divide by zero"

data DivisionResult
= DivideSuccess Float
Expand All @@ -436,9 +439,9 @@ And when we run our `usingSafeDivide` function:

```haskell
Q> usingSafeDivide 5 0
You tried to divide by zero
"You tried to divide by zero"
Q> usingSafeDivide 5 2
Your result was: 2.5
"Your result was: 2.5"
```

You may have noticed that I have referred to `case` as an expression; this is not a mistake. If we
Expand Down

0 comments on commit 81cdd0b

Please sign in to comment.