git.iko.soy/rowtype-yoga/purescript-native
on aaa6c53 help sign in
127 lines 3.0 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
17Patterns can also be used when introducing functions. For example::
19 example x y z = x * y + z
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 f _ = 0
41Literal Patterns
42----------------
44Literal patterns are provided to match on primitives::
46 f true = 0
47 f false = 1
49 g "Foo" = 0
50 g _ = 1
52 h 0 = 0
53 h _ = 1
55Variable Patterns
56-----------------
58A variable pattern matches any input and binds that input to its name::
60 double x = x * 2
62Array Patterns
63--------------
65Array patterns match an input which is an array, and bring its elements into scope. For example::
67 f [x] = x
68 f [x, y] = x * y + f xs
69 f _ = 0
71Here, the first pattern only matches arrays of length one, and brings the first element of the array into scope.
73The second pattern matches arrays with two elements, and brings the first and second elements into scope.
75Cons Patterns
76-------------
78The head and tail of a non-empty array can be matched by using a cons pattern::
80 sum [] = 0
81 sum (x : xs) = x + sum xs
83`:` associates to the right::
85 addPairs (x : y : xs) = x * y + addPairs xs
86 addPairs _ = 0
88Record Patterns
89---------------
91Record patterns match an input which is a record, and bring its properties into scope::
93 f { foo = "Foo" } = o.bar
94 f _ = 0
96Nested Patterns
97---------------
99The patterns above can be combined to create larger patterns. For example::
101 f { arr = x : _, take = "car" } = x
102 f { arr = _ : x : _, take = "cadr" } = x
103 f _ = 0
105Named Patterns
106--------------
108Named patterns bring additional names into scope when using nested patterns. Any pattern can be named by using the ``@`` symbol::
110 f a@(_ : _ : _) = true
111 f _ = false
113Here, in the first pattern, any array with two or more elements will be matched and bound to the variable ``a``.
115Guards
116------
118Guards are used to impose additional constraints inside a pattern using boolean-valued expressions, and are introduced with a pipe after the pattern::
120 evens [] = 0
121 evens (x : xs) | x % 2 == 0 = 1 + evens xs
122 evens (_ : xs) = evens xs
124When using patterns to define a function at the top level, guards appear after all patterns::
126 greater x y | x > y = true
127 greater _ _ = false