git.iko.soy/rowtype-yoga/purescript-native
on cd287bd help sign in
139 lines 3.4 kB history raw
1Pattern Matching
2================
4Pattern matching deconstructs a value to bring zero or more expressions into scope. Pattern matches are introduced with the `case` keyword.
6Pattern matches have the following general form::
8 case value of
9 pattern -> result
10 ...
11 pattern -> result
13Pattern matching can also be used in the declaration of functions, as we have already seen::
15 fn pattern_1 ... pattern_n = result
17Any 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::
19 example x (y, z) w = x * y + z * w
21The following pattern types are supported:
23- Wildcard pattern
24- Literal patterns
25- Variable pattern
26- Array patterns
27- Cons patterns
28- Record patterns
29- Named patterns
30- Guards
32Patterns need not be exhaustive. A pattern match failed exception will be thrown at runtime if no pattern matches the input.
34Wildcard Patterns
35-----------------
37The wilcard `_` matches any input and brings nothing into scope::
39 case x of
40 _ -> 0
42Literal Patterns
43----------------
45Literal patterns are provided to match on primitives::
47 case booleanValue of
48 true -> 0
49 false -> 1
51 case stringValue of
52 "Foo" -> 0
53 _ -> 1
55 case numericValue of
56 0 -> 0
57 _ -> 1
59Variable Patterns
60-----------------
62A variable pattern matches any input and binds that input to its name::
64 case foo(x) of
65 result -> bar(result)
67Array Patterns
68--------------
70Array patterns match an input which is an array, and bring its elements into scope. For example::
72 f = \arr -> case arr of
73 [x] -> x
74 [x, y] -> x * y + f xs
75 _ -> 0
77Here, the first pattern only matches arrays of length one, and brings the first element of the array into scope.
79The second pattern matches arrays with two elements, and brings the first and second elements into scope.
81Cons Patterns
82-------------
84The head and tail of a non-empty array can be matched by using a cons pattern::
86 add = \arr -> case arr of
87 [] -> 0
88 x : xs -> x + add xs
90`:` associates to the right::
92 addPairs = \arr -> case arr of
93 x : y : xs -> x * y + addPairs xs
94 _ -> 0
96Record Patterns
97---------------
99Record patterns match an input which is a record, and bring its properties into scope::
101 f = \o -> case o of
102 { foo = "Foo" } -> o.bar
103 _ -> 0
105Nested Patterns
106---------------
108The patterns above can be combined to create larger patterns. For example::
110 f = \o -> case o of
111 { arr = x : _, take = "car" } -> x
112 { arr = _ : x : _, take = "cadr" } -> x
113 _ -> 0
115Named Patterns
116--------------
118Named patterns bring additional names into scope when using nested patterns. Any pattern can be named by using the `@` symbol::
120 f = \arr -> case arr of
121 a@(_ : _ : _) -> true
122 a -> false
124Here, in the first pattern, any array with two or more elements will be matched and bound to the variable `a`.
126Guards
127------
129Guards are used to impose additional constraints inside a pattern using boolean-valued expressions, and are introduced with a pipe after the pattern::
131 evens = \arr -> case arr of
132 [] -> 0
133 x : xs | x % 2 == 0 -> 1 + evens xs
134 _ : xs -> evens xs
136When using patterns to define a function at the top level, guards appear after all patterns::
138 greater x y | x > y = true
139 greater _ _ = false