Use foldr to define filter and map

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

You'll only receive email when they publish something new.

More from T
All posts