[Haskell-beginners] map question

Brent Yorgey byorgey at seas.upenn.edu
Thu Sep 17 08:01:50 EDT 2009


On Thu, Sep 17, 2009 at 01:31:38PM +0200, Joost Kremers wrote:
>
> But I can't seem to find a way to get map to substract 1 from all members of the
> list. The following form is the only one that works, but it doesn't give the
> result I'd expect:
> 
> Prelude> map ((-) 1) [1,2,3,4]
> [0,-1,-2,-3]

By the way, the reason

  map (+1) [1,2,3,4]

works but
  
  map (-1) [1,2,3,4]

doesn't is because of an ugly corner of Haskell syntax: -1 here is
parsed as negative one, rather than an operator section with
subtraction.  The 'subtract' function is provided exactly for this
purpose, so that you can write

  map (subtract 1) [1,2,3,4]

instead.

-Brent


More information about the Beginners mailing list