git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Update patterns.rst
9ef2ea0Phil Freeman
docs/source/patterns.rst+82−104
@@ -1,27 +1,22 @@
1−## Pattern Matching
1+Pattern Matching
2+================
2
3
3Pattern matching deconstructs a value to bring zero or more expressions into scope. Pattern matches are introduced with the `case` keyword.
4Pattern matching deconstructs a value to bring zero or more expressions into scope. Pattern matches are introduced with the `case` keyword.
4
5
5−Pattern matches have the following general form
6+Pattern matches have the following general form::
6
7
7−```haskell
8−case value of
8+ case value of
9− pattern -> result
9+ pattern -> result
10− -- ...
10+ ...
11− pattern -> result
11+ pattern -> result
12−```
13
12
14−Pattern matching can also be used in the declaration of functions, as we have already seen:
13+Pattern matching can also be used in the declaration of functions, as we have already seen::
15
14
16−```haskell
17−fn pattern_1 ... pattern_n = result
15+ fn pattern_1 ... pattern_n = result
18−```
19
16
20−Any 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,
17+Any 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::
21
18
22−```haskell
23−example x (y, z) w = x * y + z * w
19+ example x (y, z) w = x * y + z * w
24−```
25
20
26The following pattern types are supported:
21The following pattern types are supported:
27
22
@@ -36,126 +31,109 @@ The following pattern types are supported:
36
31
37Patterns need not be exhaustive. A pattern match failed exception will be thrown at runtime if no pattern matches the input.
32Patterns need not be exhaustive. A pattern match failed exception will be thrown at runtime if no pattern matches the input.
38
33
39−## Wildcard Patterns
34+Wildcard Patterns
35+-----------------
40
36
41−The wilcard `_` matches any input and brings nothing into scope:
37+The wilcard `_` matches any input and brings nothing into scope::
42
38
43−```haskell
44−case x of
39+ case x of
45− _ -> 0
40+ _ -> 0
46−```
47
41
48−## Literal Patterns
42+Literal Patterns
49−
43+----------------
44+
50−Literal patterns are provided to match on primitives:
45+Literal patterns are provided to match on primitives::
51−
46+
52−```haskell
53−case booleanValue of
47+ case booleanValue of
54− true -> 0
48+ true -> 0
55− false -> 1
49+ false -> 1
56−
50+
57−case stringValue of
51+ case stringValue of
58− "Foo" -> 0
52+ "Foo" -> 0
59− _ -> 1
53+ _ -> 1
60
54
61−case numericValue of
55+ case numericValue of
62− 0 -> 0
56+ 0 -> 0
63− _ -> 1
57+ _ -> 1
64−```
65
58
66−## Variable Pattern
59+Variable Patterns
60+-----------------
67
61
68−A variable pattern matches any input and binds that input to its name:
62+A variable pattern matches any input and binds that input to its name::
69
63
70−```haskell
71−case foo(x) of
64+ case foo(x) of
72− result -> bar(result)
65+ result -> bar(result)
73−```
74
66
75−## Array Patterns
67+Array Patterns
68+--------------
76
69
77−Array patterns match an input which is an array, and bring its elements into scope.
70+Array patterns match an input which is an array, and bring its elements into scope. For example::
78
71
79−For example:
80−
81−```haskell
82−f = \arr -> case arr of
72+ f = \arr -> case arr of
83− [x] -> x
73+ [x] -> x
84− [x, y] -> x * y + f xs
74+ [x, y] -> x * y + f xs
85− _ -> 0
75+ _ -> 0
86−```
87
76
88Here, the first pattern only matches arrays of length one, and brings the first element of the array into scope.
77Here, the first pattern only matches arrays of length one, and brings the first element of the array into scope.
89
78
90The second pattern matches arrays with two elements, and brings the first and second elements into scope.
79The second pattern matches arrays with two elements, and brings the first and second elements into scope.
91
80
92−## Cons Patterns
81+Cons Patterns
93−
82+-------------
94−The head and tail of a non-empty array can be matched by using a cons pattern:
95
83
96−```haskell
84+The head and tail of a non-empty array can be matched by using a cons pattern::
97−add = \arr -> case arr of
98− [] -> 0
99− x : xs -> x + add xs
100−```
101
85
102−`:` associates to the right:
86+ add = \arr -> case arr of
87+ [] -> 0
88+ x : xs -> x + add xs
103
89
104−```haskell
90+`:` associates to the right::
105−addPairs = \arr -> case arr of
106− x : y : xs -> x * y + addPairs xs
107− _ -> 0
108−```
109
91
110−## Record Patterns
92+ addPairs = \arr -> case arr of
93+ x : y : xs -> x * y + addPairs xs
94+ _ -> 0
111
95
112−Record patterns match an input which is a record, and bring its properties into scope.
96+Record Patterns
97+---------------
113
98
114−```haskell
99+Record patterns match an input which is a record, and bring its properties into scope::
115−f = \o -> case o of
116− { foo = "Foo" } -> o.bar
117− _ -> 0
118−```
119
100
120−## Nested Patterns
101+ f = \o -> case o of
102+ { foo = "Foo" } -> o.bar
103+ _ -> 0
121
104
122−The patterns above can be combined to create larger patterns.
105+Nested Patterns
106+---------------
123
107
124−For example:
108+The patterns above can be combined to create larger patterns. For example::
125
109
126−```haskell
127−f = \o -> case o of
110+ f = \o -> case o of
128− { arr = x : _, take = "car" } -> x
111+ { arr = x : _, take = "car" } -> x
129− { arr = _ : x : _, take = "cadr" } -> x
112+ { arr = _ : x : _, take = "cadr" } -> x
130− _ -> 0
113+ _ -> 0
131−```
132
114
133−## Named Patterns
115+Named Patterns
116+--------------
134
117
135−Named patterns bring additional names into scope when using nested patterns. Any pattern can be named by using the `@` symbol:
118+Named patterns bring additional names into scope when using nested patterns. Any pattern can be named by using the `@` symbol::
136
119
137−```haskell
138−f = \arr -> case arr of
120+ f = \arr -> case arr of
139− a@(_ : _ : _) -> true
121+ a@(_ : _ : _) -> true
140− a -> false
122+ a -> false
141−```
142
123
143Here, in the first pattern, any array with two or more elements will be matched and bound to the variable `a`.
124Here, in the first pattern, any array with two or more elements will be matched and bound to the variable `a`.
144
125
145−## Guards
126+Guards
127+------
146
128
147−Guards are used to impose additional constraints inside a pattern using boolean-valued expressions, and are introduced with a pipe after the pattern:
129+Guards are used to impose additional constraints inside a pattern using boolean-valued expressions, and are introduced with a pipe after the pattern::
148
130
149−```haskell
150−evens = \arr -> case arr of
131+ evens = \arr -> case arr of
151− [] -> 0
132+ [] -> 0
152− x : xs | x % 2 == 0 -> 1 + evens xs
133+ x : xs | x % 2 == 0 -> 1 + evens xs
153− _ : xs -> evens xs
134+ _ : xs -> evens xs
154−```
155
135
156−When defining a function, guards appear after all patterns:
136+When using patterns to define a function at the top level, guards appear after all patterns::
157
137
158−```haskell
159−greater x y | x > y = true
138+ greater x y | x > y = true
160−greater _ _ = false
139+ greater _ _ = false
161−```