Going through the magnificent Haskell Programming from first principles, I'm currently reimplementing functions from standard libraries such as
Here are my current versions of them:
myMaximumBy :: (a -> a -> Ordering) -> [a] -> a -- maximumBy in Data.List also throws an error on empty lists myMaximumBy _  = error "empty list" myMaximumBy f (x:xs) = foldr (\curMax x -> if f x curMax == GT then x else curMax) x xs myMinimumBy :: (a -> a -> Ordering) -> [a] -> a myMinimumBy f = myMaximumBy $ flip f
I know they're not total functions, but I'll get to rewriting them with something like
Maybe later on.
I'm happy for any kind of feedback on how to make the code shorter, more readable, or more idiomatic.