diff --git a/examples/PatternMatching/PatternMatching.roc b/examples/PatternMatching/PatternMatching.roc index f55b5f2..c225148 100644 --- a/examples/PatternMatching/PatternMatching.roc +++ b/examples/PatternMatching/PatternMatching.roc @@ -34,34 +34,34 @@ expect expect match = \input -> when input is - [.., 42] -> EndWith42 + [.., 42] -> EndsWith42 _ -> Other - (match [24, 64, 42] == EndWith42) - && (match [42, 1, 5] != EndWith42) + (match [24, 64, 42] == EndsWith42) + && (match [42, 1, 5] != EndsWith42) # Match a list that starts with a Foo tag # followed by a Bar tag expect match = \input -> when input is - [Foo, Bar, ..] -> FooBar + [Foo, Bar, ..] -> StartsWithFooBar _ -> Other - (match [Foo, Bar, Bar] == FooBar) - && (match [Bar, Bar, Foo] != FooBar) + (match [Foo, Bar, Bar] == StartsWithFooBar) + && (match [Bar, Bar, Foo] != StartsWithFooBar) # Match a list with these exact elements: # Foo, Bar, and then (Baz "Hi") expect match = \input -> when input is - [Foo, Bar, Baz "Hi"] -> Bingo + [Foo, Bar, Baz "Hi"] -> FooBarBazStr _ -> Other - (match [Foo, Bar, Baz "Hi"] == Bingo) - && (match [Foo, Bar] != Bingo) - && (match [Foo, Bar, Baz "Hi", Blah] != Bingo) + (match [Foo, Bar, Baz "Hi"] == FooBarBazStr) + && (match [Foo, Bar] != FooBarBazStr) + && (match [Foo, Bar, Baz "Hi", Blah] != FooBarBazStr) # Match a list with Foo as its first element, and # Count for its second element. Count holds a number, @@ -69,9 +69,21 @@ expect expect match = \input -> when input is - [Foo, Count num, ..] if num > 0 -> FooBar + [Foo, Count num, ..] if num > 0 -> FooCountIf _ -> Other - (match [Foo, Count 1] == FooBar) - && (match [Foo, Count 0] != FooBar) - && (match [Baz, Count 1] != FooBar) + (match [Foo, Count 1] == FooCountIf) + && (match [Foo, Count 0] != FooCountIf) + && (match [Baz, Count 1] != FooCountIf) + +# Use `as` to create a variable equal to the part of the list that matches `..` +expect + match = \input -> + when input is + [head, .. as tail] -> HeadAndTail head tail + _ -> Other + + (match [1, 2, 3] == HeadAndTail 1 [2, 3]) + && (match [1, 2] == HeadAndTail 1 [2]) + && (match [1] == HeadAndTail 1 []) + && (match [] == Other) diff --git a/examples/PatternMatching/README.md b/examples/PatternMatching/README.md index b0bfd3d..b550d40 100644 --- a/examples/PatternMatching/README.md +++ b/examples/PatternMatching/README.md @@ -1,6 +1,28 @@ -# Pattern Matching +# Pattern Matching on Lists -Some different ways to do pattern matching in Roc. +All the ways to pattern match on lists: +```roc +when input is + [] -> EmptyList + + [_, ..] -> NonEmptyList + + ["Hi", ..] -> StartsWithHi + + [.., 42] -> EndsWith42 + + [Foo, Bar, ..] -> StartsWithFooBar + + [Foo, Bar, Baz "Hi"] -> FooBarBazStr + + [Foo, Count num, ..] if num > 0 -> FooCountIf + + [head, .. as tail] -> HeadAndTail head tail + + _ -> Other +``` +Note that this specific snippet would not typecheck because it uses lists of different types. +This is just meant to be a compact overview. See the code section below for valid Roc. ## Code ```roc @@ -14,5 +36,5 @@ Run this from the directory that has `PatternMatching.roc` in it: ``` $ roc test PatternMatching.roc -0 failed and 7 passed in 629 ms. +0 failed and 8 passed in 88 ms. ``` diff --git a/examples/index.md b/examples/index.md index fbbe314..c0bf557 100644 --- a/examples/index.md +++ b/examples/index.md @@ -8,7 +8,7 @@ You can find the source code for all of these at [github.com/roc-lang/examples]( - [FizzBuzz](/FizzBuzz/README.html) - [Basic Dict Usage](/BasicDict/README.html) - [Tuples](/Tuples/README.html) -- [Pattern Matching](/PatternMatching/README.html) +- [Pattern Matching on Lists](/PatternMatching/README.html) - [Tasks & Error Handling](/Tasks/README.html) - [Import Files](/IngestFiles/README.html) - [Import from Directory](/ImportFromDirectory/README.html)