Source: Hutton, Graham. "Programming in Haskell" (p. 267)
- Using foldMap, define a generic version of the higher-order function filter on lists that can be used with any foldable type:
filterF :: Foldable t => (a -> Bool) -> t a -> [a]
I am doing this exercise and have some questions:
- Isn't the correct type
filterF :: Foldable t => (a -> Bool) -> t a -> t a? Isn't filter supposed to preserve the structure of the container ?
This is my attempt; is it correct ? Should I place a Monoid restriction on t ?
filterF :: Foldable t => (a -> Bool) -> t a -> t a
filterF p = foldMap (\x -> if p x then pure x else mempty)