nested local declarations in ML of NJ
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: synt开发者_开发百科ax 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?
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.
加载中,请稍侯......
精彩评论