Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply a constant subtraction using map in Haskell

Tags:

haskell

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?

like image 474
OderWat Avatar asked Nov 15 '25 08:11

OderWat


1 Answers

You can use the subtract function instead

map (subtract 1) [1,2,3,4]
like image 142
Jim Jeffries Avatar answered Nov 17 '25 20:11

Jim Jeffries



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!