nested local declarations in ML of NJ

核能气质少年 提交于 2019-12-30 18:26:14

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/4297123/nested-local-declarations-in-ml-of-nj

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!