我写了这个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
其他,有没有办法这个功能可以写,将有第一个列表是按正确的顺序?
感谢您的所有努力! –