Pattern Matching
Destructuring with Patterns
Pattern matching in Gleam goes beyond simple value comparison. You can destructure complex data structures and bind parts to variables.
String Patterns
You can match on string prefixes:
case name {
"Dr. " <> rest -> "Doctor: " <> rest
"Mr. " <> rest -> "Mister: " <> rest
name -> "Person: " <> name
}
List Patterns
You can destructure lists to access their elements:
case my_list {
[] -> "empty"
[x] -> "one element: " <> int.to_string(x)
[x, y] -> "two elements"
[first, ..rest] -> "first is " <> int.to_string(first)
}
The [first, ..rest] pattern binds the first element and the remaining list.
Tuple Patterns
Tuples can be destructured in case expressions:
case #(x, y) {
#(0, 0) -> "origin"
#(x, 0) -> "on x-axis"
#(0, y) -> "on y-axis"
#(x, y) -> "at point"
}
Variable Binding
When you use a name in a pattern, it binds the matched value:
case some_value {
value if value > 10 -> "big: " <> int.to_string(value)
value -> "small: " <> int.to_string(value)
}
Let Patterns
You can also pattern match in let bindings:
let #(first, second) = #("hello", "world")
// first = "hello", second = "world"
Exhaustiveness
Gleam requires case expressions to be exhaustive -- you must handle every possible value. The compiler will tell you if you miss a case. This prevents bugs caused by unhandled scenarios.
Your Task
Write a function called describe_list that takes a list of integers and returns:
"empty"for an empty list"singleton: <n>"for a list with one element"pair: <a>, <b>"for a list with two elements"long: starts with <n>"for a list with three or more elements
Print the result for four different lists.