# Use foldr to define filter and map

January 12, 2020•122 words

## Filter

### Without foldr

```
_filter :: (a -> Bool) -> [a] -> [a]
_filter p (x:xs) | p x == True = x : _filter xs
| otherwise = _filter xs
```

### With folder

```
_filter :: (a -> Bool) -> [a] -> [a]
_filter p = foldr (\x xs -> if p x then x : xs else xs) []
```

## Map

### Without foldr

```
_map :: (a -> b) -> [a] -> [a]
_map f [] = []
_map f (x:xs) = f x : _map f xs
```

### With foldr

```
_map :: (a -> b) -> [a] -> [a]
_map f = foldr (\x xs -> f x : xs) []
```