I was trying to solve a problem that required the maximum value of a list after being mapped over by a function. The list is a range from a to b where a>b or b>a. Because Haskell can also define decreasing lists i thought that i didn’t need to check if a>b and there was no need to flip the bounds to b..a. The function looks somewhat like this:
f a b = maximum . map aFunction $ [a..b]
But if the list is decreasing i.e. a>b then Haskell gives me an exception:
Prelude.maximum: empty list
So for some reason a decreasing list hands over an empty list to the maximum function. Why is that?
I know that maximum is defined in terms of a foldl1 max and that foldl1 needs a non empty list but i don’t know why a list like [10..1] is empty when handed to a foldl1.
[a..b]desugars toenumFromTo a b. For standard numeric types (modulo a couple of quirks for floating), this keeps adding one until you are>= b. So whereb < athis is empty.You can change the increment by using the following syntax
[a,a'..b]which then takes steps in increments ofa'-a. So[10,9..1]will be what you want.