git.iko.soy/rowtype-yoga/purescript-native
on 64bd2db help sign in
161 lines 3.3 kB history raw
1## Pattern Matching
3Pattern matching deconstructs a value to bring zero or more expressions into scope. Pattern matches are introduced with the `case` keyword.
5Pattern matches have the following general form
7```haskell
8case value of
9 pattern -> result
10 -- ...
11 pattern -> result
12```
14Pattern matching can also be used in the declaration of functions, as we have already seen:
16```haskell
17fn pattern_1 ... pattern_n = result
18```
20Any of the above types of pattern are also valid when introducing functions. In addition, patterns can also be grouped in parentheses to introduce multiple-arugument functions. For example,
22```haskell
23example x (y, z) w = x * y + z * w
24```
26The following pattern types are supported:
28- Wildcard pattern
29- Literal patterns
30- Variable pattern
31- Array patterns
32- Cons patterns
33- Record patterns
34- Named patterns
35- Guards
37Patterns need not be exhaustive. A pattern match failed exception will be thrown at runtime if no pattern matches the input.
39## Wildcard Patterns
41The wilcard `_` matches any input and brings nothing into scope:
43```haskell
44case x of
45 _ -> 0
46```
48## Literal Patterns
50Literal patterns are provided to match on primitives:
52```haskell
53case booleanValue of
54 true -> 0
55 false -> 1
57case stringValue of
58 "Foo" -> 0
59 _ -> 1
61case numericValue of
62 0 -> 0
63 _ -> 1
64```
66## Variable Pattern
68A variable pattern matches any input and binds that input to its name:
70```haskell
71case foo(x) of
72 result -> bar(result)
73```
75## Array Patterns
77Array patterns match an input which is an array, and bring its elements into scope.
79For example:
81```haskell
82f = \arr -> case arr of
83 [x] -> x
84 [x, y] -> x * y + f xs
85 _ -> 0
86```
88Here, the first pattern only matches arrays of length one, and brings the first element of the array into scope.
90The second pattern matches arrays with two elements, and brings the first and second elements into scope.
92## Cons Patterns
94The head and tail of a non-empty array can be matched by using a cons pattern:
96```haskell
97add = \arr -> case arr of
98 [] -> 0
99 x : xs -> x + add xs
100```
102`:` associates to the right:
104```haskell
105addPairs = \arr -> case arr of
106 x : y : xs -> x * y + addPairs xs
107 _ -> 0
108```
110## Record Patterns
112Record patterns match an input which is a record, and bring its properties into scope.
114```haskell
115f = \o -> case o of
116 { foo = "Foo" } -> o.bar
117 _ -> 0
118```
120## Nested Patterns
122The patterns above can be combined to create larger patterns.
124For example:
126```haskell
127f = \o -> case o of
128 { arr = x : _, take = "car" } -> x
129 { arr = _ : x : _, take = "cadr" } -> x
130 _ -> 0
131```
133## Named Patterns
135Named patterns bring additional names into scope when using nested patterns. Any pattern can be named by using the `@` symbol:
137```haskell
138f = \arr -> case arr of
139 a@(_ : _ : _) -> true
140 a -> false
141```
143Here, in the first pattern, any array with two or more elements will be matched and bound to the variable `a`.
145## Guards
147Guards are used to impose additional constraints inside a pattern using boolean-valued expressions, and are introduced with a pipe after the pattern:
149```haskell
150evens = \arr -> case arr of
151 [] -> 0
152 x : xs | x % 2 == 0 -> 1 + evens xs
153 _ : xs -> evens xs
154```
156When defining a function, guards appear after all patterns:
158```haskell
159greater x y | x > y = true
160greater _ _ = false
161```