Skip to content

Commit cc1cc97

Browse files
authored
Add example of using guards with case expression (documentationjs#339)
* Add example of using guards with case expression * Apply review feedback
1 parent 4ba614e commit cc1cc97

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

language/Pattern-Matching.md

+23
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,29 @@ compare _ _ = EQ
176176

177177
(The name `otherwise` is a synonym for `true` commonly used in guards.)
178178

179+
Guards may also be used within `case` expressions, which allow for inline expressions. For example, these are equivalent:
180+
181+
```purs
182+
fb :: Int -> Effect Unit
183+
fb = log <<< case _ of
184+
n
185+
| 0 == mod n 15 -> "FizzBuzz"
186+
| 0 == mod n 3 -> "Fizz"
187+
| 0 == mod n 5 -> "Buzz"
188+
| otherwise -> show n
189+
```
190+
191+
```purs
192+
fb :: Int -> Effect Unit
193+
fb n = log x
194+
where
195+
x
196+
| 0 == mod n 15 = "FizzBuzz"
197+
| 0 == mod n 3 = "Fizz"
198+
| 0 == mod n 5 = "Buzz"
199+
| otherwise = show n
200+
```
201+
179202
Pattern Guards
180203
--------------
181204

0 commit comments

Comments
 (0)