我想要类似的东西
splitBy pred list = ( filter pred list, filter (not . pred) list )
但一次通过。
您正在寻找以下partition
功能Data.List
:
partition :: (a -> Bool) -> [a] -> ([a], [a])
它可以使用折叠很好地实现:
splitBy pred = foldr f ([], [])
where f x ~(yes, no) = if pred x then (x : yes, no)
else (yes, x : no)