[Haskell-cafe] Re: [Haskell-beginners] map question
deniz.a.m.dogan at gmail.com
Thu Sep 17 12:10:44 EDT 2009
> 2009/9/17 Joost Kremers <joostkremers at fastmail.fm>
>> Hi all,
>> I've just started learning Haskell and while experimenting with map a bit, I ran
>> into something I don't understand. The following commands do what I'd expect:
>> Prelude> map (+ 1) [1,2,3,4]
>> Prelude> map (* 2) [1,2,3,4]
>> Prelude> map (/ 2) [1,2,3,4]
>> Prelude> map (2 /) [1,2,3,4]
>> 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]
>> I know I can use an anonymous function, but I'm just trying to understand the
>> result here... I'd appreciate any hints to help me graps this.
The reason that "map (-1) [1,2,3,4]" doesn't work as you'd expect it
to is that "-" is ambiguous in Haskell (some may disagree).
"-1" means "-1" in Haskell, i.e. negative 1, not "the function that
subtracts 1 from its argument". "(-) 1" is the function that subtracts
its argument from 1, which is not what you were looking for either!
You're looking for the function that subtracts 1 from its argument,
which is `subtract 1'.
Prelude> map (subtract 1) [1..4]
Note that `subtract' is just another name for `flip (-)', i.e.
subtraction with its argument in reverse order.
More information about the Haskell-Cafe