标准名称(过滤器p xs,过滤器(not.p)xs)

时间:2012-03-22 14:03:05

标签: haskell functional-programming naming-conventions

haskell中是否有一些函数在一个列表遍历中求值(过滤p xs,filter(not.p)xs)(这里有两个)或者在函数式编程中是否有一些这种函数的通用名称? / p>

3 个答案:

答案 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])

Hoogle is your friend

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])

相关问题