NJ 的 ML 中的嵌套本地声明
大家好,我有这段代码:
local
helper(f, i, j) = local
fun NTimesF(f, n:int) =
if n = 1 then fn (x) => f(x)
else fn (x) => f(NTimesF(f, n - 1)(x));
in
if(i <= j) then NTimesF(f, i) :: helper(f, (i+1), j)
else []
end
in
fun compList f n = helper(f, 1, n);
end;
我需要编写一个程序,它接收一些函数 f 和整数 n 并生成函数列表,例如 [f1, f2, ... fn] <- fn 是函数的组合n 次,但每次我收到一个错误:
- stdIn:1.1-2.9 Error: syntax error: deleting LOCAL ID LPAREN
stdIn:2.10-2.14 Error: syntax error: deleting COMMA ID COMMA
stdIn:2.16-2.25 Error: syntax error: deleting RPAREN EQUALOP LOCAL
stdIn:3.6-3.17 Error: syntax error: deleting FUN ID
stdIn:4.6-4.10 Error: syntax error: deleting IF ID
stdIn:4.15-4.22 Error: syntax error: deleting THEN FN
stdIn:4.27-4.31 Error: syntax error: deleting DARROW ID
stdIn:5.6-5.13 Error: syntax error: deleting ELSE FN
stdIn:5.16-5.22 Error: syntax error: deleting RPAREN DARROW ID
stdIn:6.8-7.8 Error: syntax error: deleting IN IF
stdIn:7.17-7.29 Error: syntax error: deleting THEN ID
stdIn:8.6-8.13 Error: syntax error: deleting ELSE LBRACKET RBRACKET
stdIn:9.8-11.5 Error: syntax error: deleting END IN FUN
似乎我的嵌套本地声明是错误的,有人可以解释一下为什么吗?
hello everyone I have this snippet of the code:
local
helper(f, i, j) = local
fun NTimesF(f, n:int) =
if n = 1 then fn (x) => f(x)
else fn (x) => f(NTimesF(f, n - 1)(x));
in
if(i <= j) then NTimesF(f, i) :: helper(f, (i+1), j)
else []
end
in
fun compList f n = helper(f, 1, n);
end;
I need to write program which receives some function f and integer n and produce list of functions such as [f1, f2, ... fn] <- fn is the composition of the function n times but every time I receive an error:
- stdIn:1.1-2.9 Error: syntax error: deleting LOCAL ID LPAREN
stdIn:2.10-2.14 Error: syntax error: deleting COMMA ID COMMA
stdIn:2.16-2.25 Error: syntax error: deleting RPAREN EQUALOP LOCAL
stdIn:3.6-3.17 Error: syntax error: deleting FUN ID
stdIn:4.6-4.10 Error: syntax error: deleting IF ID
stdIn:4.15-4.22 Error: syntax error: deleting THEN FN
stdIn:4.27-4.31 Error: syntax error: deleting DARROW ID
stdIn:5.6-5.13 Error: syntax error: deleting ELSE FN
stdIn:5.16-5.22 Error: syntax error: deleting RPAREN DARROW ID
stdIn:6.8-7.8 Error: syntax error: deleting IN IF
stdIn:7.17-7.29 Error: syntax error: deleting THEN ID
stdIn:8.6-8.13 Error: syntax error: deleting ELSE LBRACKET RBRACKET
stdIn:9.8-11.5 Error: syntax error: deleting END IN FUN
it seems that my nested local declarations are wrong, can somebody please explain why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 SML 中定义局部函数和变量有两种方法:
local ... in ... end
和let ... in ... end
。local
和let
之间的区别在于,对于local
来说,in
和end
之间的区别> 是一个或多个变量或函数声明。对于let
来说,in
和end
之间是一个表达式。与
local
不同,let
是一个表达式,let
表达式的值是in
之间表达式的值和结束
。由于在您的情况下,您在
in
和end
之间有一个表达式(并且您希望函数计算该表达式的结果),因此您需要使用let
,而不是本地
。There are two ways to define local functions and variables in SML:
local ... in ... end
andlet ... in ... end
.The difference between
local
andlet
is that withlocal
what comes betweenin
andend
are one or more variable or function declarations. Withlet
what comes betweenin
andend
is an expression.Unlike
local
,let
is an expression and the value of alet
expression is the value of the expression betweenin
andend
.Since in your case you have an expression between
in
andend
(and you want the function to evaluate to the result of that expression), you need to uselet
, notlocal
.