If we want to display a function that increments each element of the range by 1, we could write
map (\x -> x + 1) [1..5]
but I think most people will just go for
map (+1) [1..5]
instead of this. But this obviously does not work with (-1), since this one is negative.
So, the first thing that came to mind was
map (+(-1)) [1..5]
which will make sense considering how subtraction is defined in Prelude ( x - y = x + negate y ), but it looks a bit strange to me. Then i came up with
map (flip (-) 1) [1..5]
It somehow looks better for me, but maybe it is too complicated.
Now I know this doesn't matter, but I wonder if I am missing a more obvious way to write this? If not, which of the two methods would you prefer? I just ask because often these small details like this make your code more idiomatic and therefore enjoyable for other developers who need to read it.
Decision
Now that I have received a couple of answers, I think my personal favorite
map (subtract 1) [1..5]
followed by
map pred [1..5]
mainly because the first one is really clear, and no one should guess / look for what pred (predecessor) means.
coding-style haskell
Michael Kohl Dec 15 '10 at 20:29 2010-12-15 20:29
source share