示例:split [1;3;2;4;7;9];;
输出:([1;3;7;9], [2;4])
我是 F# 新手,无法理解。
不能使用内置的 partition
函数。
这是我目前的代码:
let rec split xs =
match xs with
| [] -> [], []
| xs -> xs, []
| xh::xt -> let odds, evens = split xt
if (xh % 2) = 0 then xh::odds, xh::evens
else xh::odds, evens
修复后的代码:
let rec split xs =
match xs with
| [] -> [], []
| xh::xt -> let odds, evens = split xt
if (xh % 2) = 0 then odds, xh::evens
else xh::odds, evens
*感谢@TheInnerLight指出我的错误:无法到达的情况和不必要地修改几率
https://dev59.com/5aLia4cB1Zd3GeqPhVb8/
。根据你的教授对寻求帮助的政策,你可能需要或不需要这样做,但完全透明地披露任何在完成作业时得到的帮助总是一个好主意。 - rmunn