递归函数和 "let in" 错误

Recursive function and "let in" errors

我想弄清楚这个函数的作用,当我在我的代码编辑器中 运行 它时,它在 "end in" 处给我一个语法错误。我试过在 "let x" 和其他一些地方加上括号,但我不知所措。如果能帮助我理解错误原因,我将不胜感激。

let rec map (f: 'a -> 'b) (y: 'a list): 'b list =
  begin match y with
  | [] -> []
  | h :: t -> (f h) :: (map f t)
  end in
 let x = map (fun t -> (t + 1) [0; 1; 2] in
 0 :: x

这是您想要完成的吗?

let () =
  let rec map f y =
    match y with
    | [] -> []
    | h :: t -> (f h) :: (map f t)
  in
  let x = map (fun t -> t + 1) [0; 1; 2] in
  0 :: x
  |> List.iter (fun x -> Printf.printf "%d\n" x)