打印语句后变量被遗忘
variable gets forgotten after print statement
为什么编译?
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
我认为您对语言的挫败感比语言的局限性更能阻止您解决问题。正如我在之前的回答中所说,分号不能像您使用的那样使用。您需要将这些语句括在括号内:
fun foo (h::t) =
(
PolyML.print (h::t);
print "\n";
h = hd(t)
)
此外,您的第一个片段不需要分号:
fun foo (h::t) =
h = hd(t)
事情是这样的,在 SML 中,分号不用于终止语句,而是用于分隔表达式。将 ;
视为二元运算符,就像 +
或 -
一样。随着添加的约束,您需要括号。
此外,您可能在 h = hd(t)
中以错误的方式使用了 =
运算符。这不是赋值,而是相等性检查,就像其他语言中的 ==
一样。如果要分配,则需要 ref
类型。
最好问问你到底想解决什么问题,因为此时你完全误解了 SML 的语法和语义,我们不能在这里写教程。
为什么编译?
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
我认为您对语言的挫败感比语言的局限性更能阻止您解决问题。正如我在之前的回答中所说,分号不能像您使用的那样使用。您需要将这些语句括在括号内:
fun foo (h::t) =
(
PolyML.print (h::t);
print "\n";
h = hd(t)
)
此外,您的第一个片段不需要分号:
fun foo (h::t) =
h = hd(t)
事情是这样的,在 SML 中,分号不用于终止语句,而是用于分隔表达式。将 ;
视为二元运算符,就像 +
或 -
一样。随着添加的约束,您需要括号。
此外,您可能在 h = hd(t)
中以错误的方式使用了 =
运算符。这不是赋值,而是相等性检查,就像其他语言中的 ==
一样。如果要分配,则需要 ref
类型。
最好问问你到底想解决什么问题,因为此时你完全误解了 SML 的语法和语义,我们不能在这里写教程。