在 Ocaml 中的递归函数内调用递归函数时出现语法错误

Syntax Error when calling Recursive Function inside a Recursive Function in Ocaml

编译器returns在

中使用命令时出现语法错误
xyz aux
    if((match4242 aux) = 0) then main (!list) else 1

这是我的完整代码。

open Printf
open Format

let regraUm m = m/2
  
let regraDois m = ((m / 10) mod 10) * (m mod 10)

let regraTres m = 42


let match4242 list =
    let a = ref 0 in
    let rec match42 list = 
    match list with
    |[]->[]
    |m::body->
    begin
    if (m = 42) then a := 1;
    match42 body
    end
    in match42 list;
    !a

let rec main aux = 
    let list = ref [] in
    let rec xyz aux = 
        let () = List.iter (fun x -> printf "%d " x) aux in
        match aux with
        |[]->[]
        |m::body -> 
        begin
        if ((m mod 2) = 0) then list := (m - (regraUm m))::!list;
        if ((m mod 3) = 0) || ((m mod 4) = 0) then 
            if (regraDois m <> 0) then
                list := (m - (regraDois m)) ::!list;
        if ((m mod 5) = 0) then list := (m - (regraTres m))::!list;
        xyz body
        end
    in xyz aux
    if((match4242 aux) = 0) then main (!list) else 1

程序检查 42 是否在列表中,如果不在列表中,则它会按照一组除法、减法等规则再次调用自身。 不知道最后这个信息对调试这段代码是否有帮助。

这两行:

   xyz aux
if ((match4242 aux) = 0) then main (!list) else 1

表示单个表达式,因为它们没有被 ; 分隔。但实际上你不能在这个位置(函数参数)有一个 if 表达式,除非你把它括起来。

您很可能想要 ;aux 之后。