I recently ran into following problem: Given a list of numbers, I want to subtract a constant from every entry of this list. The natural but wrong thing to do is
map (-3) [5,6,7]
Here the - is not interpreted a binary function but as an unary negation operator.
I found following workarounds:
map (+(-3)) [5,6,7]
map ((+)(-3)) [5,6,7]
map (flip (-) 3) [5,6,7]
Question : Is there a more elegant way to do this that avoids this unnecessary pile of parenthesis?
Nope, sorry. You have the subtract function, but it's not shorter.
Prelude> map (subtract 3) [3,4,5]
[0,1,2]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With