【问题标题】:variable gets forgotten after print statement打印语句后变量被遗忘
【发布时间】:2016-04-23 02:57:48
【问题描述】:

为什么会编译?

fun foo (h::t) =
    h = hd(t);

但这不是

fun foo (h::t) =
    PolyML.print (h::t);
    print "\n";
    h = hd(t);

?

Value or constructor (h) has not been declared   Found near =( h, hd(t))

Value or constructor (t) has not been declared   Found near =( h, hd(t))
Exception- Fail "Static errors (pass2)" raised

【问题讨论】:

    标签: sml smlnj


    【解决方案1】:

    我认为您对语言的挫败感比语言的局限性更能阻止您解决问题。正如我在之前的回答中所说,分号不能像您使用的那样使用。您需要将这些语句括在括号内:

    fun foo (h::t) =
      (
        PolyML.print (h::t);
        print "\n";
        h = hd(t)
      )
    

    此外,您首先 sn-p 不需要分号:

    fun foo (h::t) =
        h = hd(t)
    

    事情是这样的,在 SML 中,分号不用于终止语句,它们用于分隔表达式。将; 视为二元运算符,就像+-。加上你需要括号的附加约束。

    另外,您可能在h = hd(t) 中以错误的方式使用= 运算符。这不是赋值,而是相等检查,就像其他语言中的 == 一样。如果你想分配,你需要一个ref 类型。

    最好问问你到底想解决什么问题,因为此时你完全误解了 SML 的语法和语义,我们不能在这里写教程。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-22
      • 1970-01-01
      • 2018-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多