-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpatterns.ail
More file actions
123 lines (102 loc) · 1.95 KB
/
Copy pathpatterns.ail
File metadata and controls
123 lines (102 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
-- patterns.ail - Comprehensive pattern matching examples
-- Demonstrates all pattern types supported in AILANG
-- =======================
-- TUPLE PATTERNS
-- =======================
-- Simple tuple destructuring
match (1, 2) {
(x, y) => x + y
}
-- Output: 3
-- Nested tuples
match ((1, 2), (3, 4)) {
((a, b), (c, d)) => a + b + c + d
}
-- Output: 10
-- =======================
-- LITERAL PATTERNS
-- =======================
-- Integer literals
match 42 {
0 => "zero",
42 => "forty-two",
_ => "other"
}
-- Output: "forty-two"
-- String literals
match "hello" {
"hello" => "greeting",
"goodbye" => "farewell",
_ => "unknown"
}
-- Output: "greeting"
-- Boolean literals
match true {
true => "yes",
false => "no"
}
-- Output: "yes"
-- =======================
-- VARIABLE PATTERNS
-- =======================
-- Binding values to variables
match 100 {
x => x * 2
}
-- Output: 200
-- =======================
-- WILDCARD PATTERNS
-- =======================
-- Catch-all pattern
match 999 {
0 => "zero",
1 => "one",
_ => "many"
}
-- Output: "many"
-- =======================
-- ADT CONSTRUCTOR PATTERNS
-- =======================
type Option[a] = Some(a) | None
-- Matching constructors with fields
match Some(42) {
Some(n) => n * 2,
None => 0
}
-- Output: 84
-- Matching nullary constructors
match None {
Some(n) => n,
None => -1
}
-- Output: -1
-- =======================
-- NESTED PATTERNS
-- =======================
type Result[a, e] = Ok(a) | Err(e)
-- Nested ADT patterns
match Ok(Some(42)) {
Ok(Some(n)) => n,
Ok(None) => 0,
Err(e) => -1
}
-- Output: 42
-- Tuple with ADT patterns
match (Some(1), Some(2)) {
(Some(x), Some(y)) => x + y,
(Some(x), None) => x,
(None, Some(y)) => y,
(None, None) => 0
}
-- Output: 3
-- =======================
-- COMPLEX EXAMPLES
-- =======================
-- Pattern matching in expressions
let result = match (10, 20) {
(0, y) => y,
(x, 0) => x,
(x, y) => x + y
} in
result
-- Output: 30