git.iko.soy/rowtype-yoga/purescript-native
on 79fcdf4 help sign in
139 lines 3.2 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- Constructor patterns
29- Record patterns
30- Named patterns
31- Guards
33Patterns need not be exhaustive. A pattern match failed exception will be thrown at runtime if no pattern matches the input.
35Wildcard Patterns
36-----------------
38The wilcard `_` matches any input and brings nothing into scope::
40 f _ = 0
42Literal Patterns
43----------------
45Literal patterns are provided to match on primitives::
47 f true = 0
48 f false = 1
50 g "Foo" = 0
51 g _ = 1
53 h 0 = 0
54 h _ = 1
56Variable Patterns
57-----------------
59A variable pattern matches any input and binds that input to its name::
61 double x = x * 2
63Array Patterns
64--------------
66Array patterns match an input which is an array, and bring its elements into scope. For example::
68 f [x] = x
69 f [x, y] = x * y
70 f _ = 0
72Here, the first pattern only matches arrays of length one, and brings the first element of the array into scope.
74The second pattern matches arrays with two elements, and brings the first and second elements into scope.
76Cons Patterns
77-------------
79The head and tail of a non-empty array can be matched by using a cons pattern::
81 sum [] = 0
82 sum (x : xs) = x + sum xs
84``:`` associates to the right::
86 addPairs (x : y : xs) = x * y + addPairs xs
87 addPairs _ = 0
90Constructor patterns
91--------------------
93Constructor patterns match a data constructor and its arguments::
95 data Foo = Foo String | Bar Number Boolean
97 foo (Foo s) = true
98 foo (Bar _ b) = b
100Record Patterns
101---------------
103Record patterns match an input which is a record, and bring its properties into scope::
105 f { foo = "Foo", bar = n } = n
106 f _ = 0
108Nested Patterns
109---------------
111The patterns above can be combined to create larger patterns. For example::
113 f { arr = x : _, take = "car" } = x
114 f { arr = _ : x : _, take = "cadr" } = x
115 f _ = 0
117Named Patterns
118--------------
120Named patterns bring additional names into scope when using nested patterns. Any pattern can be named by using the ``@`` symbol::
122 f a@(_ : _ : _) = true
123 f _ = false
125Here, in the first pattern, any array with two or more elements will be matched and bound to the variable ``a``.
127Guards
128------
130Guards are used to impose additional constraints inside a pattern using boolean-valued expressions, and are introduced with a pipe after the pattern::
132 evens [] = 0
133 evens (x : xs) | x % 2 == 0 = 1 + evens xs
134 evens (_ : 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