:: (a -> Bool) -> [a] -> [a] package:base

filter, applied to a predicate and a list, returns the list of those elements that satisfy the predicate; i.e.,
filter p xs = [ x | x <- xs, p x]

Examples

>>> filter odd [1, 2, 3]
[1,3]
>>> filter (\l -> length l > 3) ["Hello", ", ", "World", "!"]
["Hello","World"]
>>> filter (/= 3) [1, 2, 3, 4, 3, 2, 1]
[1,2,4,2,1]
takeWhile, applied to a predicate p and a list xs, returns the longest prefix (possibly empty) of xs of elements that satisfy p.

Laziness

>>> takeWhile (const False) undefined
*** Exception: Prelude.undefined
>>> takeWhile (const False) (undefined : undefined)
[]
>>> take 1 (takeWhile (const True) (1 : undefined))
[1]

Examples

>>> takeWhile (< 3) [1,2,3,4,1,2,3,4]
[1,2]
>>> takeWhile (< 9) [1,2,3]
[1,2,3]
>>> takeWhile (< 0) [1,2,3]
[]
dropWhile p xs returns the suffix remaining after takeWhile p xs.

Examples

>>> dropWhile (< 3) [1,2,3,4,5,1,2,3]
[3,4,5,1,2,3]
>>> dropWhile (< 9) [1,2,3]
[]
>>> dropWhile (< 0) [1,2,3]
[1,2,3]
The dropWhileEnd function drops the largest suffix of a list in which the given predicate holds for all elements.

Laziness

This function is lazy in spine, but strict in elements, which makes it different from reverse . dropWhile p . reverse, which is strict in spine, but lazy in elements. For instance:
>>> take 1 (dropWhileEnd (< 0) (1 : undefined))
[1]
>>> take 1 (reverse $ dropWhile (< 0) $ reverse (1 : undefined))
*** Exception: Prelude.undefined
but on the other hand
>>> last (dropWhileEnd (< 0) [undefined, 1])
*** Exception: Prelude.undefined
>>> last (reverse $ dropWhile (< 0) $ reverse [undefined, 1])
1

Examples

>>> dropWhileEnd isSpace "foo\n"
"foo"
>>> dropWhileEnd isSpace "foo bar"
"foo bar"

>>> dropWhileEnd (> 10) [1..20]
[1,2,3,4,5,6,7,8,9,10]