55

If we want to map a function that increases every element of a range by 1, we could write

map (\x -> x + 1) [1..5]

but I guess most people would just go for

map (+1) [1..5]

instead. But this obviously doesn't work with (-1) since that's negative one.

So the first thing that came to mind was

map (+(-1)) [1..5]

which would make sense considering how subtraction is defined in the Prelude (x - y = x + negate y), but looks a bit odd to me. I then I came up with

map (flip (-) 1) [1..5]

This somehow looks better to me, but is maybe a bit too complicated.

Now I know this no big deal, but I'm wondering if I'm missing a more obvious way to write this? If not, which of the 2 ways would you prefer? I'm really just asking because often it's small details like this that make your code more idiomatic and hence pleasant for other developers who have to read it.

Solution

Now that I got a couple of answers, I think my personal favorite is

map (subtract 1) [1..5]

followed by

map pred [1..5]

mostly because the first one is really explicit and nobody needs to guess/look up what pred means (predecessor).

Michael Kohl
  • 66,324
  • 14
  • 138
  • 158

5 Answers5

49

You can use the subtract function instead of - if you want to right-section subtraction:

map (subtract 1) [1..5]
Anon.
  • 58,739
  • 8
  • 81
  • 86
9

Since - is both the infix subtract and the prefix negate, you can't use the (*x) (where * is an infix operator and x a value) syntax for -. Luckily Prelude comes with negate and subtract, which is \x -> -x and \x y -> y-x respectively, so that you may use those where you need to differentiate between the two.

HaskellElephant
  • 9,819
  • 4
  • 38
  • 67
3

After many years since this question was asked, in GHC 9 we now have the LexicalNegation extension which allows the section (- 1), as long we use whitespace to separate the minus sign from the number.

Indeed, after enabling the extension, we have:

> map (subtract 1) [1..5]       -- still works, of course
[0, 1, 2, 3, 4]

> map (- 1) [1..5]              -- with whitespace
[0, 1, 2, 3, 4]                 -- (- 1) is now a section

> map (-1) [1..5]               -- no whitespace
*error*                         -- (-1) is now a negative literal
chi
  • 111,837
  • 3
  • 133
  • 218
2

I don't like subtract because it's confusingly backwards. I'd suggest

minus :: Num n => n -> n -> n
minus = (-)
infixl 6 `minus`

Then you can write

map (`minus` 1) [1..5]
dfeuer
  • 48,079
  • 5
  • 63
  • 167
  • 4
    I prefer just `(+(-1))`. The trick with the new name is nice, but it takes up a valuable name. Too bad ``(`-` 1)`` is invalid syntax. – Will Ness Aug 28 '14 at 16:49
2

I think map (\x -> x - 1) [1..5] transmits the programmer's intention better, since there's no doubt about what is being subtracted from what. I also find your first solution, map (+(-1)) [1..5], easy to read too.

carnieri
  • 1,353
  • 6
  • 11