Why the haskell map does not work with (-1)?

Viewed 115

What was wrong with the following code?

map (+1) [1, 2, 3 ] ==  map (\x -> x + 1) [1, 2, 3]
map (1+) [1, 2, 3] == map (\x -> 1 + x) [1, 2, 3]
map (-1) [1, 2, 3]  == map (\x -> x - 1) [1, 2, 3]
map (1-) [1, 2, 3] == map (\x -> 1 - x) [1, 2, 3]

Why the following does not work?

map (-1) [1, 2, 3]
1 Answers

(-1) is interpreted as a number: minus one. You can use subtract :: Num a => a -> a -> a to subtract a value:

map (subtract 1) [1, 2, 3]  == map (\x -> x - 1) [1, 2, 3]
Related