# Filter function

A higher-order function for filtering elements of a list

## Illustration

Let's filter a given list of numbers to only retain the odd numbers. In Language:Haskell, we would use the filter function as follows:

```Prelude> filter odd [1,2,3,4,5]
[1,3,5]
```

That is, filter is applied to a predicate (i.e., a function to return a Boolean) and a list; filter returns the list of those elements that satisfy the predicate. The higher-order function filter can be defined as follows:

```-- Define filter via pattern matching
filter :: (a -> Bool) -> [a] -> [a]
filter p [] = []
filter p (x:xs) = if p x then x : ys else ys
where
ys = filter p xs
```

For what it matters, the filter function could also be defined in terms of the fold function, thereby reusing a recursion scheme as opposed to the explicitly recursive definition above.

```-- Define filter via foldr
filter' p = foldr f []
where
f x = if p x then (:) x else id
```

Further, we can also define the filter function in terms of list comprehension; in this manner we hint at the meaning of list comprehensions because, in fact, list comprehensions correspond to syntactic sugar whose elimination also involves the Filter function.

```-- Define filter via list comprehension
filter'' :: (a -> Bool) -> [a] -> [a]
filter'' p xs = [ x | x <- xs, p x]
```

## User contributions

This user never has never made submissions.

## Syntax for editing wiki

For you are available next options:

will make text bold.

will make text italic.

will make text underlined.

will make text striked.

will allow you to paste code headline into the page.

will allow you to link into the page.

will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

will allow you to paste image into the page.

is list with bullets.

is list with numbers.

will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

will allow your to insert code snippets from @worker.

## Syntax for editing wiki

For you are available next options:

will make text bold.

will make text italic.

will make text underlined.

will make text striked.

will allow you to paste code headline into the page.

will allow you to link into the page.

will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

will allow you to paste image into the page.

is list with bullets.

is list with numbers.

will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

will allow your to insert code snippets from @worker.