1个案例中的多个模式
Multiple Patterns in 1 case
在 SML 中,是否可以在一个 case 语句中使用多个模式?
例如,我有 4 个算术运算符用字符串表示,"+", "-", "*", "/"
我想打印 "PLUS MINUS"
其中的 "+" or "-"
和 "MULT DIV"
如果它是 "*" or "/"
.
TL;DR:有什么地方可以简化以下内容以减少用例吗?
case str of
"+" => print("PLUS MINUS")
| "-" => print("PLUS MINUS")
| "*" => print("MULT DIV")
| "/" => print("MULT DIV")
在标准 ML 中,没有。在 ML 的其他方言中,例如 OCaml,是的。在某些情况下,您可能会考虑将模式匹配拆分为单独的 cases/functions,或者跳过模式匹配以支持更短的包罗万象的表达式,例如
if str = "+" orelse str = "-" then "PLUS MINUS" else
if str = "*" orelse str = "/" then "MULT DIV" else ...
鉴于您已经用 smlnj tag, then yes, SML/NJ supports this kind of patterns. They call it or-patterns 标记了您的问题,它看起来像这样:
case str of
("+" | "-") => print "PLUS MINUS"
| ("*" | "/") => print "MULT DIV"
注意括号。
MLton 的 master 分支也支持它,作为其 Successor ML effort 的一部分,但您必须自己编译 MLton。
val str = "+"
val _ =
case str of
"+" | "-" => print "PLUS MINUS"
| "*" | "/" => print "MULT DIV"
请注意,MLton 不需要括号。现在使用此命令编译它(与 SML/NJ 不同,您必须在 MLton 中显式启用此功能):
mlton -default-ann 'allowOrPats true' or-patterns.sml
扩展 Ionuş 的示例,您甚至可以将数据类型与其中的其他类型一起使用,但它们的类型(和标识符分配)必须匹配:
datatype mytype = COST as int | QUANTITY as int | PERSON as string | PET as string;
case item of
(COST n|QUANTITY n) => print Int.toString n
|(PERSON name|PET name) => print name
如果类型或名称不匹配,它将被拒绝:
case item of
(COST n|PERSON n) => (* fails because COST is int and PERSON is string *)
(COST n|QUANTITY q) => (* fails because the identifiers are different *)
这些模式也适用于函数定义:
fun myfun (COST n|QUANTITY n) = print Int.toString n
|myfun (PERSON name|PET name) = print name
;
在 SML 中,是否可以在一个 case 语句中使用多个模式?
例如,我有 4 个算术运算符用字符串表示,"+", "-", "*", "/"
我想打印 "PLUS MINUS"
其中的 "+" or "-"
和 "MULT DIV"
如果它是 "*" or "/"
.
TL;DR:有什么地方可以简化以下内容以减少用例吗?
case str of
"+" => print("PLUS MINUS")
| "-" => print("PLUS MINUS")
| "*" => print("MULT DIV")
| "/" => print("MULT DIV")
在标准 ML 中,没有。在 ML 的其他方言中,例如 OCaml,是的。在某些情况下,您可能会考虑将模式匹配拆分为单独的 cases/functions,或者跳过模式匹配以支持更短的包罗万象的表达式,例如
if str = "+" orelse str = "-" then "PLUS MINUS" else
if str = "*" orelse str = "/" then "MULT DIV" else ...
鉴于您已经用 smlnj tag, then yes, SML/NJ supports this kind of patterns. They call it or-patterns 标记了您的问题,它看起来像这样:
case str of
("+" | "-") => print "PLUS MINUS"
| ("*" | "/") => print "MULT DIV"
注意括号。
MLton 的 master 分支也支持它,作为其 Successor ML effort 的一部分,但您必须自己编译 MLton。
val str = "+"
val _ =
case str of
"+" | "-" => print "PLUS MINUS"
| "*" | "/" => print "MULT DIV"
请注意,MLton 不需要括号。现在使用此命令编译它(与 SML/NJ 不同,您必须在 MLton 中显式启用此功能):
mlton -default-ann 'allowOrPats true' or-patterns.sml
扩展 Ionuş 的示例,您甚至可以将数据类型与其中的其他类型一起使用,但它们的类型(和标识符分配)必须匹配:
datatype mytype = COST as int | QUANTITY as int | PERSON as string | PET as string;
case item of
(COST n|QUANTITY n) => print Int.toString n
|(PERSON name|PET name) => print name
如果类型或名称不匹配,它将被拒绝:
case item of
(COST n|PERSON n) => (* fails because COST is int and PERSON is string *)
(COST n|QUANTITY q) => (* fails because the identifiers are different *)
这些模式也适用于函数定义:
fun myfun (COST n|QUANTITY n) = print Int.toString n
|myfun (PERSON name|PET name) = print name
;