Reputation: 31918
I want to implement minimum with foldr or foldMap. According to the exercise, it should have this definition:
mini :: (Foldable t, Ord a) => t a -> Maybe a -- named "mini" to avoid name clash
It sounded pretty straightforward, but I do not know what I can substiture for X below to make it work. Help please?
mini xs = Just (foldr min X xs)
And you get bonus points for showing me how to do it with foldMap too, but that seems harder.
Upvotes: 5
Views: 772
Reputation: 144136
You could do:
mini :: (Foldable f, Ord a) => f a -> Maybe a
mini = foldr maybeMin Nothing
where
maybeMin x Nothing = Just x
maybeMin x (Just y) = Just (min x y)
Upvotes: 5
Reputation: 1
You could try by replacing 'X' with the first element on the list. Of course you should now deal with the case when the function 'mini' gets invoked with an empty list.
Upvotes: -1