SML中的嵌套case语句 [英] Nested case statements in SML

查看:129
本文介绍了SML中的嵌套case语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个风格上的问题。给定以下代码:

This is more of a stylistic question than anything else. Given the following piece of code:

case e1 of                    (* datatype type_of_e1  = p1 | p2 *)
    p1 => case e11 of         (* datatype type_of_e11 = NONE | SOME int *)
              NONE => expr11
            | SOME v => expr12 v
  | p2 => case e21 of         (* datatype type_of_e21 = NONE | SOME string *)
              NONE => expr21
            | SOME v => expr22 v

有没有办法解决规则类型同意是由于尝试将 e11 匹配到 p2 而引起的错误,而不是将 p1 的表达式括在括号中? p2 模式还有另一个case语句,以避免'只是切换模式'答案;-)。

Is there a way to resolve the types of rules don't agree error caused by trying to pattern match e11 to p2, other than enclosing p1's expression in parenthesis? The p2 pattern has another case statement, to avoid the 'just switch the patterns' answer ;-).

更新:更改了代码以反映更具体的情况

update: changed the code to reflect a more concrete case

推荐答案

否。标准ML定义中的句法规则规定,案例表达式的匹配项尝试最大程度地消耗潜在子句。并且由于该语言中没有结束语或类似的标记,因此解析器会欣喜地吃掉您提供的每个 | pat => exp子句,直到看到终止匹配子句列表的内容为止。

No. The syntactic rules in the Definition of Standard ML state that the match arms of a case expression attempt to maximally consume potential clauses. And since there's no "end case" or similar marker in the language, the parser will merrily eat each of the "| pat => exp" clauses that you feed it until it sees something that terminates a list of match clauses.

这篇关于SML中的嵌套case语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆