【发布时间】:2011-11-19 20:40:55
【问题描述】:
我很肯定有更好的方法来成对交换列表中的项目( [1;2;3;4] -> [2;1;4;3] ),因为我做的太多了根据我的喜好附加,但我不确定如何最好地做到这一点。
let swapItems lst =
let f acc item =
match acc with
| [] -> [item]
| hd :: next :: tl when tl <> [] -> [next] @ tl @ [item;hd]
| _ -> item :: acc
List.fold f [] lst
我该如何改进呢?这仅适用于具有偶数长度的列表。
【问题讨论】:
标签: f# functional-programming sequences