haskell中是否有一些函数在一个列表遍历中求值(过滤p xs,filter(not.p)xs)(这里有两个)或者在函数式编程中是否有一些这种函数的通用名称? / p>
答案 0 :(得分:33)
首先看一下你需要的类型:
Prelude> :t \p xs -> (filter p xs, filter (not . p) xs)
\p xs -> (filter p xs, filter (not . p) xs)
:: (a -> Bool) -> [a] -> ([a], [a])
Prelude> :hoogle (a -> Bool) -> [a] -> ([a], [a])
Prelude break :: (a -> Bool) -> [a] -> ([a], [a])
Prelude span :: (a -> Bool) -> [a] -> ([a], [a])
Data.List break :: (a -> Bool) -> [a] -> ([a], [a])
Data.List partition :: (a -> Bool) -> [a] -> ([a], [a])
Data.List span :: (a -> Bool) -> [a] -> ([a], [a])
现在试试这些功能:
Prelude> break odd [1..10]
([],[1,2,3,4,5,6,7,8,9,10])
Prelude> span odd [1..10]
([1],[2,3,4,5,6,7,8,9,10])
Prelude> import Data.List
Prelude Data.List> partition odd [1..10]
([1,3,5,7,9],[2,4,6,8,10])
答案 1 :(得分:3)
Haskell称之为partition
。
答案 2 :(得分:0)
我想你想要Data.List.partition,例如
partition (>2) [1,2,3,4,5]
结果为([3,4,5], [1,2])