# The `match` block is a simple way to perform (somewhat) elegant pattern matching in Paisley.
# It's similar to the `switch` statement in other languages, but with two key differences:
# 1. Logic does not "fall through" from one case to the next.
# 2. It can check for more than just equality; any boolean operation can be used.
# Here's an example that's most similar to a `switch` statement:
match {random_int(1,7)} do
print Monday if 1
print Tuesday if 2
print Wednesday if 3
print Thursday if 4
print Friday if 5
print Saturday if 6
print Sunday if 7
else
print "Invalid day"
end
# But a match block can also do more fuzzy matching:
match {random_int(40,100)} do
print Grade: A if {>= 90}
print Grade: B if {>= 80}
print Grade: C if {>= 70}
print Grade: D if {>= 60}
else
print Grade: F
end
# And it can match against text patterns:
match 'hello123' do
if {like '^%d+$'} then print "Input contains only digits" end
if {like '^%a+$'} then print "Input contains only letters" end
if {like '^%w+$'} then print "Input contains only letters and digits" end
else print "Input contains some non-letter and non-digit characters" end
# Nested match blocks are of course possible:
let weather = "sunny"
let temperature = {random_int(0, 40)}
match {weather} do
if "sunny" then
match {temperature} do
if {< 20} then print "It's a cool sunny day" end
if {>= 20} then print "It's a warm sunny day" end
else print "Temperature out of range" end
end
if "rainy" then print "It's a rainy day" end
if "cloudy" then print "It's a cloudy day" end
else print "Unknown weather condition" end