Nested local ads in ML NJ

Hi everyone, I have this piece of 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 fn = helper(f, 1, n); end; 

I need to write a program that receives some function f and integer n and creates a list of functions, such as [f1, f2, ... fn] <- fn - the composition of the function n times, but every time I get 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 someone explain why?

+7
sml smlnj
source share
1 answer

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 happens between in and end is one or more declarations of variables or functions. With let , what happens between in and end is an expression.

Unlike local , let is the expression, and the value of the expression a let 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 the result of this expression), you need to use let , not local .

+20
source share

All Articles