I'm just learning Haskell and am kind of stuck.
I'd like to compare list elements and measure the difference between them and return the highest one.
Unfortunatly, I do not know how to approach that problem.
For usual, I'd just iterate the list and compare the neighbours but that does not seem to be the way to go in Haskell.
I already tried using map
but as I said I do not really know how you can solve that problem.
I'd be thankful for every kind of advice!
Best wishes
Edit: My idea is to first zip all pairs like this pairs a = zip a (tail a)
. Then I'd like to get all differences (maybe with map
?) and then just chose the highest one. I just can't handle the Haskell syntax.
I don't know what you mean by "measure the discrepancy" between list elements, but if you want to calculate the "largest" element in a list, you'd use the built-in
maximum
function:This function takes a list of values that can be ordered, so all numbers, chars, and strings, among others.
If you want to get the difference between the maximum value and the minimum value, you can use the similar function
minimum
, then just subtract the two. Sure, there might be a slightly faster solution whereby you only traverse the list once, or you could sort the list then take the first and last elements, but for most cases doingdiff xs = maximum xs - minimum xs
is plenty fast enough and makes the most sense to someone else.So what you want to do is compute a difference between successive elements, not calculate the minimum and maximum of each element. You don't need to index directly, but rather use a handy function called
zipWith
. It takes a binary operation and two lists, and "zips" them together using that binary operation. So something likeIt is rather handy because if one of the lists runs out early, it just stops there. So you could do something like
But how do we offset the list by 1? Well, the easy (and safe) way is to use
drop 1
. You could usetail
, but it'll throw an error and crash your program ifxs
is an empty list, butdrop
will notSo an example would be
This function will return positive and negative values, and we're interested only in the magnitude, so we can then use the
abs
function:And then using the function I highlighted above:
And you're done! If you want to be fancy, you could even write this in point-free notation as
Now, this will in fact raise an error on an empty list because
maximum []
throws an error, but I'll let you figure out a way to solve that.