NJ 的 ML 中的嵌套本地声明

发布于 2024-10-04 14:55:19 字数 1347 浏览 2 评论 0原文

大家好,我有这段代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪 2024-10-11 14:55:19

在 SML 中定义局部函数和变量有两种方法:local ... in ... endlet ... in ... end

locallet 之间的区别在于,对于 local 来说,inend 之间的区别> 是一个或多个变量或函数声明。对于 let 来说,inend 之间是一个表达式。

local 不同,let 是一个表达式,let 表达式的值是 in 之间表达式的值和结束

由于在您的情况下,您在 inend 之间有一个表达式(并且您希望函数计算该表达式的结果),因此您需要使用 let,而不是本地

There are two ways to define local functions and variables in SML: local ... in ... end and let ... in ... end.

The difference between local and let is that with local what comes between in and end are one or more variable or function declarations. With let what comes between in and end is an expression.

Unlike local, let is an expression and the value of a let expression is the value of the expression between in and end.

Since in your case you have an expression between in and end (and you want the function to evaluate to the result of that expression), you need to use let, not local.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文