Skip to content

Update Rewrites.hs #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Conversation

RodideBoer
Copy link

I was struggling with the same issue for myMinimumBy and myMaximumBy and then it struck me. It specifically states that it returns the last value the comparison returned GT for (in case of maximum) so if all comparisons return LT you should end up at the last value and just return that, just like the proposed example inputs/outputs suggests. That's how I got the idea to do a foldl and start with the last value of the list. And the other way around for minimum.

I was struggling with the same issue for myMinimumBy and myMaximumBy and then it struck me. It specifically states that it returns the last value the comparison returned GT for (in case of maximum) so if all comparisons return LT you should end up at the last value and just return that, just like the proposed example inputs/outputs suggests. That's how I got the idea to do a foldl and start with the last value of the list. And the other way around for minimum.
@dwayne
Copy link
Owner

dwayne commented Feb 20, 2017

@RodideBoer Thanks for the contribution. Your submission got me thinking that all I needed then was for the initial value to be the last and everything should work. And, indeed it does:

myMaximumBy :: (a -> a -> Ordering) -> [a] -> a
myMaximumBy f xs = foldr (\a b -> if f a b == GT then a else b) (last xs) xs

myMinimumBy :: (a -> a -> Ordering) -> [a] -> a
myMinimumBy f xs = foldr (\a b -> if f a b == LT then a else b) (last xs) xs

@dwayne dwayne closed this Feb 20, 2017
dwayne added a commit that referenced this pull request Feb 20, 2017
Thanks to the PR #1 submitted by @RodideBoer that led me to think
of it.
dwayne added a commit that referenced this pull request Feb 20, 2017
Thanks to PR #1 submitted by @RodideBoer that led
me to think of it.
@RodideBoer RodideBoer deleted the patch-1 branch February 24, 2017 13:51
@ethagnawl
Copy link

I'm a bit relieved to know I wasn't the only person having issues with this exercise! 🥂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants