3

I recently ran into following problem: Given a list of numbers, I want to subtract a constant from every entry of this list. The natural but wrong thing to do is

map (-3) [5,6,7]

Here the - is not interpreted a binary function but as an unary negation operator.

I found following workarounds:

map (+(-3)) [5,6,7]
map ((+)(-3)) [5,6,7]
map (flip (-) 3) [5,6,7]

Question : Is there a more elegant way to do this that avoids this unnecessary pile of parenthesis?

flawr
  • 10,814
  • 3
  • 41
  • 71

1 Answers1

3

Nope, sorry. You have the subtract function, but it's not shorter.

Prelude> map (subtract 3) [3,4,5]
[0,1,2]
Filip Haglund
  • 13,919
  • 13
  • 64
  • 113