This page is available as PDF here.
Pattern Matching
Pattern matching in Epos replaces traditional if-else statements and provides powerful control flow based on value structure.
Basic Match Expressions
The fundamental syntax uses match ... then ... end
:
number: int = 42
message: string = match number then
0 => "zero"
1 => "one"
42 => "the answer"
=> "something else"
_ end
Multiple Value Matching
Match against multiple possible values:
day: int = 3
-type: string = match day then
day1, 2, 3, 4, 5 => "weekday"
6, 7 => "weekend"
=> "invalid day"
_ end
Boolean Matching
Pattern match on boolean conditions:
age: int = 20
status: string = match age >= 18 then
true => "adult"
false => "minor"
end
List Pattern Matching
Match on list structure and contents:
numbers: list(int) = {1, 2, 3}
empty: list(int) = {}
result: string = match numbers then
=> "empty list"
empty {1} => "just one"
{1, 2} => "two elements starting with 1"
=> "something else"
_ end
# Pattern match with list length
fn describe-list(items: list(t)): string
(items) then
match len0 => "empty"
1 => "single item"
=> "multiple items"
_ end
end
Using Match in Control Flow
Since Epos doesn’t have traditional loops, use pattern matching with recursion:
fn countdown(n: int)
(n)
print<= 0 then
match n true => print("Done!")
false => countdown(n - 1)
end
end
fn process-list(items: list(string), index: int = 0)
< len(items) then
match index true => {
(elem(items, index))
print-list(items, index + 1)
process}
false => print("All done")
end
end
Record Pattern Matching
Match on record structures and fields:
Status
record code: int
message: string
end
fn handle-response(status: Status): string
.code then
match status200 => "Success: #{status.message}"
404 => "Not Found"
500 => "Server Error: #{status.message}"
=> "Unknown status: #{status.code}"
_ end
end
response: Status = @{
=> 200,
code => "OK"
message }
= handle-response(response) result :
Nested Pattern Matching
Pattern matching can be nested for complex logic:
User
record name: string
age: int
-admin: bool
isend
fn check-access(user: User): string
.is-admin then
match usertrue => "Full access granted"
false => match user.age >= 18 then
true => "Limited access granted"
false => "Access denied"
end
end
end
Pattern Matching with Functions
Use pattern matching to create different behaviors:
fn fibonacci(n: int): int
match n then0 => 0
1 => 1
=> fibonacci(n - 1) + fibonacci(n - 2)
_ end
end
fn factorial(n: int): int
<= 1 then
match n true => 1
false => n * factorial(n - 1)
end
end
Expression-Based Control
Since match is an expression, it can be used anywhere a value is expected:
# In variable assignment
-rate: float = match income then
tax10000 => 0.0
50000 => 0.15
=> 0.25
_ end
# In function arguments
(match weather then
print"sunny" => "Wear sunglasses!"
"rainy" => "Bring an umbrella!"
=> "Have a nice day!"
_ end)
# As return values
fn get-discount(customer-type: string): float
-type then
match customer"premium" => 0.20
"regular" => 0.10
"new" => 0.05
=> 0.0
_ end
end
Pattern matching is the primary control flow mechanism in Epos, replacing traditional if-else statements and loops with more expressive and functional constructs.
Next, learn about advanced features in Epos.
Next page →