2012-03-22 8 views

Respuesta

33

Primera mirada en el tipo que usted necesita:

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

Ahora probar las funciones:

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

+1 para mostrar cómo encontrarlo usted mismo. –

+2

¿Quién va a hacer que el sitio "Déjame hoogle eso para ti"? –

+2

Así es cómo se obtiene el comando ': hoogle' en ghci: http://www.haskell.org/haskellwiki/Hoogle#GHCi_Integration – amindfv

0

Creo que quieres Data.List.partition, p.

partition (>2) [1,2,3,4,5] 

resultados en ([3,4,5], [1,2])

Cuestiones relacionadas