Search code examples
syntaxsyntax-errorocamlsml

SML: syntax error: replacing FUN with VAL


I am trying to write a function in ml that works like a for loop (yes i know its not how the language is supposed to work). So here is my code so far:

fun for (f:int->unit) start_i:int end_i:int = 
let fun for2 (f:int->unit) start_i:int end_i:int i:int =
    if i=end_i - 1 then
        f i
    else
        (f i;
        for2 f start_i end_i (i + 1))
in
    for2 f start_i end_i start_i
end

But sml (and Ocaml too) is giving me this error:

test.ml:1.2-1.5 Error: syntax error: replacing  FUN with  VAL
test.ml:2.6-2.9 Error: syntax error: replacing  FUN with  VAL

So, there is something wrong with my function's signature. But I can't find what it is. Can you help me?

Thanks


Solution

  • Not sure if this also compiles in ocaml (is f#) but a version of this where interactive is not complaining about is:

    let rec for2 (body: int->unit) iStart iEnd =
        if iStart = iEnd then ()
        else
            body iStart;
            for2 body (iStart+1) iEnd