【发布时间】:2015-09-03 04:39:00
【问题描述】:
我编写了这个 F# 函数来将列表分区到某个点,而不是进一步 - 很像 takeWhile 和 partition 之间的交叉。
let partitionWhile c l =
let rec aux accl accr =
match accr with
| [] -> (accl, [])
| h::t ->
if c h then
aux (h::accl) t
else
(accl, accr)
aux [] l
唯一的问题是“被拿走”的物品被颠倒了:
> partitionWhile ((>=) 5) [1..10];;
val it : int list * int list = ([5; 4; 3; 2; 1], [6; 7; 8; 9; 10])
除了调用rev之外,有没有一种方法可以编写这个函数来让第一个列表的顺序正确?
【问题讨论】: