
Am Donnerstag 17 September 2009 13:31:38 schrieb 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.
(-) a b = a - b, so (((-) 1) x) = 1 - x and you've mapped (\x -> 1-x) over the list. You want to map (\x -> x-1), which is map (subtract 1) list or map (flip (-) 1) list (or map (+ (-1)) list)
TIA
Joost