【发布时间】:2011-12-18 01:30:21
【问题描述】:
我想做一个函数,它接受一个列表并返回两个列表:第一个包含所有奇数项,第二个包含每个偶数项。
例如,给定[1;2;4;6;7;9],我想返回[ [1;4;7] ; [2;6;9] ]。
到目前为止,我已经写了这篇文章,但我不知道如何进步。
let splitList list =
let rec splitOdd oList list1 list2 =
match oList with
| [] -> []
| head :: tail -> splitEven tail (list1::head) list2
and splitEven oList list1 list2 =
match oList with
| [] -> []
| head :: tail -> splitOdd tail list1 (list2::head)
splitOdd list [] []
【问题讨论】:
-
你不能使用 List.partition 吗?
-
@Mathias: 不,
List.partition使用谓词,在这种情况下没有合适的谓词。
标签: list f# functional-programming ocaml tail-recursion