Exercise: Pattern Matching
Now let's practice pattern matching. Implement a classify function that returns an appropriate description string based on an Option<Int64> value.
Requirements:
Some(n)wheren > 0→ return"positive: n"(showing the specific value)Some(0)→ return"zero"Some(n)wheren < 0→ return"negative: n"(showing the specific value)None→ return"none"
You need to use a match expression, enum patterns (matching Some and None), and pattern guards. Apply what you've learned to write the complete function body.
Note
Cangjie's
Option<T> is a built-in type with two constructors: Some(T) and None. Some(x) can match and bind the inner value to variable x.