File tree Expand file tree Collapse file tree 1 file changed +2
-13
lines changed Expand file tree Collapse file tree 1 file changed +2
-13
lines changed Original file line number Diff line number Diff line change @@ -65,21 +65,10 @@ squishAgain = squishMap id
65
65
-- It takes a comparison function and a list and returns the greatest element of
66
66
-- the list based on the last value that the comparison returned GT for
67
67
myMaximumBy :: (a -> a -> Ordering ) -> [a ] -> a
68
- myMaximumBy comp l@ (x: xs) =
69
- foldr (\ a b -> if comp a b == GT then a else b) x l
70
- -- It doesn't produce the desired result for:
71
- -- myMaximumBy (\_ _ -> LT) [1..10]
72
- -- => 1 (should be 10)
68
+ myMaximumBy f xs = foldr (\ a b -> if f a b == GT then a else b) (last xs) xs
73
69
74
70
-- 11
75
71
-- It takes a comparison function and a list and returns the least element of
76
72
-- the list based on the last value that the comparison returned LT for
77
73
myMinimumBy :: (a -> a -> Ordering ) -> [a ] -> a
78
- myMinimumBy comp l@ (x: xs) =
79
- foldr (\ a b -> if comp a b == LT then a else b) x l
80
- -- It doesn't produce the desired result for:
81
- -- myMinimumBy (\_ _ -> GT) [1..10]
82
- -- => 1 (should be 10)
83
-
84
- -- N.B. The only way I know how to get a value of type a is by taking the value
85
- -- from the given list because we don't know anything about the type a.
74
+ myMinimumBy f xs = foldr (\ a b -> if f a b == LT then a else b) (last xs) xs
You can’t perform that action at this time.
0 commit comments