I'll give a hint, by way of a simpler problem to solve first. I would like to have a helper function with the type helper :: (Num a, Ord a) => (a, [a]) -> (a, [a])
. The helper should return the same answer as this function:
inefficientHelper (x, xs) = (minimum xs, map (\x' -> x'-x) xs)
The problem with inefficientHelper
is that it computes its two answers in two separate passes, so it could never be a component that we could use in implementing the function you care about. So your first task is to implement helper
to return the right value (as specified by this inefficient implementation), but while making only one pass over the list xs
.
Presumably this is being given as an exercise in some teaching material, and so the most recent material will have other examples of tying the knot that can serve as a hint for what comes after that. But if not, then have a Google -- there's lots of writing floating around on "tying the knot".