Reputation: 5739
I simply want to substract 1 from every list element in [1,2,3,4] like
map (-1) [1,2,3,4] -- does not work
map ((-)1) [1,2,3,4] -- gives [0,-1,-2,-3] which is "-listvalue" + 1
map (\x = x - 1) [1,2,3,4] -- what I want but so verbose
map (+ (-1)) [1,2,3,4] -- well :)
map (flip (-) 1) [1,2,3,4] -- I heard of flip before
(map . flip (-)) 1 [1,2,3,4] -- Kinda nice mapped substration composing thingy
map pred [1,2,3,4] -- OK cheated and won't help with -2
Do I miss a form which would be "correct" and / or what Mr. Haskell use?
Upvotes: 4
Views: 1440
Reputation: 30237
You've run into a minor wart of Haskell's design. The expression (-1)
could in principle be interpreted in two ways:
1
.Haskell goes with #2. To make up for this the subtract
function is provided to cover case #1.
Upvotes: 4
Reputation: 10081
You can use the subtract function instead
map (subtract 1) [1,2,3,4]
Upvotes: 6