Let's start again with the type.
filter :: (a -> Bool) -> [a] -> [a]
The first parameter is a function that maps values from type a to Bool. The second parameter is a list of as. The result type is a list of as.
We can create a list of as only from the second parameter. The function is used to decide whether an element of the second parameter should be included in the result.
Start ghci and try out the following lines:
:t filter
:t even
:t filter even
filter even [1..10]
- Use filter on the knights list defined in the previous lecture to filter knights whose favourite colour is green or red.
- Complete the following function implementation:
myfilter :: (a -> Bool) -> [a] -> [a]