是否可以对变量而不是常量值进行模式匹配:
# let x = 2 in
let y = 5 in
match 2 with
| x -> "foo"
| y -> "bar"
| _ -> "baz";;
let y = 5 in
Warning 26: unused variable y.
let x = 2 in
Warning 26: unused variable x.
| y -> "bar"
Warning 11: this match case is unused.
| _ -> "baz";;
Warning 11: this match case is unused.
- : string = "foo"
显然,使用这种语法,x -> "foo"
case 会处理一切。有没有办法让它等同于:
match 2 with
| 2 -> "foo"
| 5 -> "bar"
| _ -> "baz"
匹配表达式的值是在运行时确定的?