9

我想要类似的东西

splitBy pred list = ( filter pred list, filter (not . pred) list )

但一次通过。

4

1 回答 1

15

您正在寻找以下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)
于 2013-02-19T23:52:59.300 回答