OCaml中相邻元素的成对交换

OCaml中相邻元素的成对交换,ocaml,Ocaml,到目前为止我有 let flipeven(listname)= let newlist= [] in let first=0 in let second=1 in let stop= List.length(listname)-1 in let rec flipevenhelper (x,y)= if (second<= stop) then insert((List.nth(listname) x), newList) in insert

到目前为止我有

let flipeven(listname)=
    let newlist= [] in
    let first=0 in
    let second=1 in 
    let stop= List.length(listname)-1 in
let rec flipevenhelper (x,y)=
if (second<= stop) then
    insert((List.nth(listname) x), newList) in
    insert((List.nth(listname) y), newList) in 
    let second=second+2 in
    let first=first+2 in
    flipevenhelper (first, second)
else
    newList;;
in关键字始终与let关键字配对。这不是连接两个表达式的一般方式,这似乎是您想要的

第二;OCaml中的运算符将两个表达式合并为一个表达式。第一个表达式被计算,但随后被忽略。它应该具有单位类型。对第二个表达式求值,其值为组合表达式的值

注意,;在某种意义上,优先级低于if/then/else。因此,如果使用;接线员

下面是一个小例子:

# if 3 > 2 then Printf.printf "yes\n"; 4 else 5;;
Error: Syntax error

# if 3 > 2 then (Printf.printf "yes\n"; 4) else 5;;
yes
- : int = 4

在修复语法之后,仍然有许多事情需要修复。特别是,您应该认识到OCaml中的变量和列表是不可变的。您不能仅通过调用insert将newList插入列表。

我尝试在没有任何LET的情况下将行更改为具有单个分号,但仍然收到相同的错误您的代码中有许多错误。我认为可以通过在代码后面加括号来消除这个语法错误。但是你还有很多事情要解决。我会更新我的答案。我最近看到3个几乎相同的OCaml语法问题,比如。在完成任务之前,请安装自动压头,并学习一些OCaml入门教程!!
# if 3 > 2 then Printf.printf "yes\n"; 4 else 5;;
Error: Syntax error

# if 3 > 2 then (Printf.printf "yes\n"; 4) else 5;;
yes
- : int = 4