2020-10-10 01:10:16 +03:00
# Lambda case syntax
This function takes a single argument and immediately pattern matches on it. As we'll see below, it can be written using `cases` syntax:
2024-07-10 17:46:18 +03:00
``` unison
2020-10-10 01:10:16 +03:00
isEmpty x = match x with
[] -> true
_ -> false
```
2024-07-10 19:17:15 +03:00
``` ucm
2020-10-10 01:10:16 +03:00
2023-12-22 14:55:24 +03:00
Loading changes detected in scratch.u.
2020-10-10 01:10:16 +03:00
I found and typechecked these definitions in scratch.u. If you
do an `add` or `update` , here's how your codebase would
change:
⍟ These new definitions are ok to `add` :
2020-10-22 05:47:25 +03:00
isEmpty : [t] -> Boolean
2020-10-10 01:10:16 +03:00
```
Here's the same function written using `cases` syntax:
2024-07-10 17:46:18 +03:00
``` unison
2020-10-10 01:10:16 +03:00
isEmpty2 = cases
[] -> true
_ -> false
```
2024-07-10 19:17:15 +03:00
``` ucm
2020-10-10 01:10:16 +03:00
2023-12-22 14:55:24 +03:00
Loading changes detected in scratch.u.
2020-10-10 01:10:16 +03:00
I found and typechecked these definitions in scratch.u. If you
do an `add` or `update` , here's how your codebase would
change:
⍟ These new definitions are ok to `add` :
2020-10-22 05:47:25 +03:00
isEmpty2 : [t] -> Boolean
2020-10-10 01:10:16 +03:00
(also named isEmpty)
```
Notice that Unison detects this as an alias of `isEmpty` , and if we view `isEmpty`
2024-07-10 19:17:15 +03:00
``` ucm
2024-06-12 01:22:09 +03:00
scratch/main> view isEmpty
2020-10-10 01:10:16 +03:00
2020-10-22 05:47:25 +03:00
isEmpty : [t] -> Boolean
2020-10-10 01:10:16 +03:00
isEmpty = cases
2021-11-18 22:47:46 +03:00
[] -> true
_ -> false
2020-10-10 01:10:16 +03:00
```
2020-10-10 01:22:15 +03:00
it shows the definition using `cases` syntax opportunistically, even though the code was originally written without that syntax.
2020-10-10 01:10:16 +03:00
## Multi-argument cases
2020-10-28 00:21:37 +03:00
Functions that take multiple arguments and immediately match on a tuple of arguments can also be rewritten to use `cases` . Here's a version using regular `match` syntax on a tuple:
2020-10-10 01:10:16 +03:00
2024-07-10 17:46:18 +03:00
``` unison
2020-10-10 01:10:16 +03:00
merge : [a] -> [a] -> [a]
merge xs ys = match (xs, ys) with
([], ys) -> ys
(xs, []) -> xs
(h +: t, h2 +: t2) ->
if h < = h2 then h +: merge t (h2 +: t2)
else h2 +: merge (h +: t) t2
```
2024-07-10 19:17:15 +03:00
``` ucm
2024-06-12 01:22:09 +03:00
scratch/main> add
2020-10-10 01:10:16 +03:00
⍟ I've added these definitions:
2021-06-08 21:50:04 +03:00
merge : [a] -> [a] -> [a]
2020-10-10 01:10:16 +03:00
```
2020-10-28 00:21:37 +03:00
And here's a version using `cases` . The patterns are separated by commas:
2020-10-10 01:10:16 +03:00
2024-07-10 17:46:18 +03:00
``` unison
2020-10-10 01:10:16 +03:00
merge2 : [a] -> [a] -> [a]
merge2 = cases
2020-10-28 00:21:37 +03:00
[], ys -> ys
xs, [] -> xs
h +: t, h2 +: t2 ->
2020-10-10 01:10:16 +03:00
if h < = h2 then h +: merge2 t (h2 +: t2)
else h2 +: merge2 (h +: t) t2
```
2024-07-10 19:17:15 +03:00
``` ucm
2020-10-10 01:10:16 +03:00
2023-12-22 14:55:24 +03:00
Loading changes detected in scratch.u.
2020-10-10 01:10:16 +03:00
I found and typechecked these definitions in scratch.u. If you
do an `add` or `update` , here's how your codebase would
change:
⍟ These new definitions are ok to `add` :
2021-06-08 21:50:04 +03:00
merge2 : [a] -> [a] -> [a]
2020-10-10 01:22:15 +03:00
(also named merge)
2020-10-10 01:10:16 +03:00
```
2020-10-10 01:22:15 +03:00
Notice that Unison detects this as an alias of `merge` , and if we view `merge`
2020-10-10 01:10:16 +03:00
2024-07-10 19:17:15 +03:00
``` ucm
2024-06-12 01:22:09 +03:00
scratch/main> view merge
2020-10-10 01:10:16 +03:00
merge : [a] -> [a] -> [a]
merge = cases
2022-11-22 19:06:28 +03:00
[], ys -> ys
xs, [] -> xs
2021-11-18 22:47:46 +03:00
h +: t, h2 +: t2 ->
if h < = h2 then h +: merge t (h2 +: t2)
else h2 +: merge (h +: t) t2
2020-10-10 01:10:16 +03:00
```
2020-10-28 00:21:37 +03:00
it again shows the definition using the multi-argument `cases` syntax opportunistically, even though the code was originally written without that syntax.
Here's another example:
2024-07-10 17:46:18 +03:00
``` unison
2021-08-24 00:05:37 +03:00
structural type B = T | F
2020-10-28 00:21:37 +03:00
2023-03-02 17:11:13 +03:00
blah : B -> B -> Text
2020-10-28 00:21:37 +03:00
blah = cases
T, x -> "hi"
2023-01-17 17:55:24 +03:00
x, y -> "bye"
2020-10-28 00:21:37 +03:00
2021-05-12 18:35:33 +03:00
blorf = cases
x, T -> x
2023-01-17 17:55:24 +03:00
x, y -> y
2021-05-12 18:35:33 +03:00
2020-10-28 00:21:37 +03:00
> blah T F
> blah F F
2021-05-12 18:35:33 +03:00
> blorf T F
2020-10-28 00:21:37 +03:00
```
2024-07-10 19:17:15 +03:00
``` ucm
2020-10-28 00:21:37 +03:00
2023-12-22 14:55:24 +03:00
Loading changes detected in scratch.u.
2020-10-28 00:21:37 +03:00
I found and typechecked these definitions in scratch.u. If you
do an `add` or `update` , here's how your codebase would
change:
⍟ These new definitions are ok to `add` :
2021-08-24 00:05:37 +03:00
structural type B
2023-03-02 17:11:13 +03:00
blah : B -> B -> Text
2021-05-12 18:35:33 +03:00
blorf : B -> B -> B
2020-10-28 00:21:37 +03:00
Now evaluating any watch expressions (lines starting with
`>` )... Ctrl+C cancels.
2023-03-02 17:11:13 +03:00
12 | > blah T F
2021-05-12 18:35:33 +03:00
⧩
"hi"
2023-03-02 17:11:13 +03:00
13 | > blah F F
2021-05-12 18:35:33 +03:00
⧩
"bye"
2020-10-28 00:21:37 +03:00
2023-03-02 17:11:13 +03:00
14 | > blorf T F
2021-05-12 18:35:33 +03:00
⧩
F
2020-10-28 00:21:37 +03:00
```
2021-07-20 07:45:03 +03:00
## Patterns with multiple guards
2024-07-10 17:46:18 +03:00
``` unison
2021-07-20 07:45:03 +03:00
merge3 : [a] -> [a] -> [a]
merge3 = cases
[], ys -> ys
xs, [] -> xs
2021-07-20 07:50:16 +03:00
h +: t, h2 +: t2 | h < = h2 -> h +: merge3 t (h2 +: t2)
| otherwise -> h2 +: merge3 (h +: t) t2
2021-07-20 07:45:03 +03:00
```
2024-07-10 19:17:15 +03:00
``` ucm
2021-07-20 07:45:03 +03:00
2023-12-22 14:55:24 +03:00
Loading changes detected in scratch.u.
2021-07-20 07:45:03 +03:00
I found and typechecked these definitions in scratch.u. If you
do an `add` or `update` , here's how your codebase would
change:
⍟ These new definitions are ok to `add` :
merge3 : [a] -> [a] -> [a]
```
2024-07-10 19:17:15 +03:00
``` ucm
2024-06-12 01:22:09 +03:00
scratch/main> add
2021-07-20 07:45:03 +03:00
⍟ I've added these definitions:
merge3 : [a] -> [a] -> [a]
2024-06-12 01:22:09 +03:00
scratch/main> view merge3
2021-07-20 07:45:03 +03:00
merge3 : [a] -> [a] -> [a]
merge3 = cases
2021-11-18 22:47:46 +03:00
[], ys -> ys
xs, [] -> xs
2021-11-19 08:30:03 +03:00
h +: t, h2 +: t2
2021-11-19 19:46:18 +03:00
| h < = h2 -> h +: merge3 t (h2 +: t2)
2021-11-19 08:30:03 +03:00
| otherwise -> h2 +: merge3 (h +: t) t2
2021-07-20 07:45:03 +03:00
```
2021-07-20 07:50:16 +03:00
This is the same definition written with multiple patterns and not using the `cases` syntax; notice it is considered an alias of `merge3` above.
2024-07-10 17:46:18 +03:00
``` unison
2021-07-20 07:50:16 +03:00
merge4 : [a] -> [a] -> [a]
merge4 a b = match (a,b) with
[], ys -> ys
xs, [] -> xs
h +: t, h2 +: t2 | h < = h2 -> h +: merge4 t (h2 +: t2)
h +: t, h2 +: t2 | otherwise -> h2 +: merge4 (h +: t) t2
```
2024-07-10 19:17:15 +03:00
``` ucm
2021-07-20 07:50:16 +03:00
2023-12-22 14:55:24 +03:00
Loading changes detected in scratch.u.
2021-07-20 07:50:16 +03:00
I found and typechecked these definitions in scratch.u. If you
do an `add` or `update` , here's how your codebase would
change:
⍟ These new definitions are ok to `add` :
merge4 : [a] -> [a] -> [a]
(also named merge3)
```