
2009/9/17 Joost Kremers
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] [2,3,4,5] Prelude> map (* 2) [1,2,3,4] [2,4,6,8] Prelude> map (/ 2) [1,2,3,4] [0.5,1.0,1.5,2.0] Prelude> map (2 /) [1,2,3,4] [2.0,1.0,0.6666666666666666,0.5]
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]
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.
TIA
Joost
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] [0,1,2,3] Note that `subtract' is just another name for `flip (-)', i.e. subtraction with its argument in reverse order. -- Deniz Dogan