我是SMLNJ的新手。这有效:
case (tts t2) of
(other, "<+") => parse_append t t2 ts pre c j
| (other, "<<-") => parse_sample t t2 ts pre c j
| (other, "<-") => parse_assign_or_sub t t2 ts pre c j
| (_, _) => error t2 "parse_stmt: Expected sample, append or assignment operator."
(other
类型为&#34;数据类型为tokentype&#34;,t和t2为令牌,tts
将令牌映射到一对(tokentype, content)
。其余为上下文 - 具体而不重要。)
但它并不是非常模块化的。特别是当我在多个地方与同一个运营商进行匹配时,我并不希望它们在所有地方重复出现。所以我尝试了这个,这不起作用:
(* this should go in a separate file of declarations *)
structure Ops = struct
val APPEND = "<+"
val SAMPLE = "<<-"
val ASSIGN = "<-"
end
(* this stays in the code *)
case (tts t2) of
(other, Op.APPEND) => parse_append t t2 ts pre c j
| (other, Op.SAMPLE) => parse_sample t t2 ts pre c j
| (other, Op.ASSIGN) => parse_assign_or_sub t t2 ts pre c j
| (_, _) => error t2 "parse_stmt: Expected sample, append or assignment operator."
显然,您无法对变量的值进行模式匹配(在某种程度上这是有道理的)。有什么样的&#34;常数&#34;声明我可以应用于运算符定义,以便我可以将它们的值放在一个单独的模块中,还是有其他一些很好的方法在SMLNJ中执行此操作?
答案 0 :(得分:1)
你可以让它们成为构造函数:
datatype Op = APPEND | ...
然后将您的信息流标记为
datatype Token = Op of Op | Id of string | ...
然后您的模式匹配可以是
case (tts t2) of
(other, APPEND) => ...