【问题标题】:Error SML: unbound variable or constructor错误 SML:未绑定的变量或构造函数
【发布时间】:2018-03-26 03:38:10
【问题描述】:

我是 SML 的新手。我尝试在下面创建和测试以下函数,但收到错误消息。不知道是什么问题。

fun isOld(pFirstTuple: int*int*int, pSecondTuple: int*int*int) = 
    if (#1 pFirstTuple) < (#1 pSecondTuple)
    then 
        true
    if (#1 pFirstTuple) = (#1 pSecondTuple)
    then 
        if (#2 pFirstTuple) < (#2 pSecondTuple)
        then
            true

    else
        false

我试过这个命令“val p = isOld((8,9,10),(10,11,12))”,但它显示了以下错误未绑定变量或构造函数 .我该如何解决这个问题?

【问题讨论】:

    标签: sml


    【解决方案1】:

    您的代码如下所示,忽略了各种子表达式(将它们替换为 ABC

    if A
    then true
    if B
    then if C
         then true
    else false
    

    您正在大量使用if/then/else,但语法并不完全正确。在 SML 中,每个 if 必须 有一个 then else 子句与之关联。这是我对您的实际意思的猜测:

    if A
    then true
    else if B
    then if C
         then true
         else false
    else false
    

    这开始变得非常混乱——但您可以使用布尔逻辑来清理它。请注意,例如,if X then true else false 与简单地编写 X 的含义完全相同,因为这两个表达式都是 bool 类型,并且无论X 是什么,都将始终计算为相同的布尔值。你可以扩展这个推理来看看

    1. if X then true else Y 等价于 X orelse Y
    2. if X then Y else false 等同于 X andalso Y

    有了这些想法,我们可以大大清理你的代码:

    A orelse (B andalso C)
    

    【讨论】:

      猜你喜欢
      • 2021-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-04
      • 2017-12-09
      • 2021-02-17
      相关资源
      最近更新 更多