Ocaml 为什么我的函数使用模式匹配而不使用if-then-else?

Ocaml 为什么我的函数使用模式匹配而不使用if-then-else?,ocaml,Ocaml,与…相比 let rec parity n = if (n = 0) then print_string "even" else if (n = 1) print_string "odd" else parity (n-2);; 我对这种语言还是新手。if-then-else中的错误特别出现在我的print\u string语句上,带有语法错误。您的第二个如果缺少则 (顺

与…相比

let rec parity n = if (n = 0) then 
                    print_string "even" else if (n = 1)
                    print_string "odd" else 
                    parity (n-2);;

我对这种语言还是新手。if-then-else中的错误特别出现在我的print\u string语句上,带有语法错误。

您的第二个
如果
缺少


(顺便说一句,
if
后面的表达式不需要在OCaml中加括号。)

这是漫长的一天……谢谢!
let rec parity n = 
match n with
| 0 -> "even"
| 1 -> "odd"
| _ -> parity(n-2);;