Correct a function that maps a list of negative numbers.
Input: [-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0]
Output: [-11,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1]
A function that I wrote but that results in a mistake.
map (-1) [-10..0]
This can be solved using lambda
map (\x -> x - 1) [-10..0]
The challenge is to solve it with partial application.
Top comments (8)
Personally, I think the lambda solution is perfect without partial application because it is easy to read. But for the purposes of this challenge, here is a partial application solution.
I don't have a Haskell environment to test with, but I tested the equivalent in F#.
It works, I checked. That's a pretty clever solution.
Doh, I missed the more obvious solution. 🙃
Very close.
This one gives:
Huh, this works verbatim in F#.
No HKT or type classes, so each structure in F# has its own
map
implementation.I'm not sure why it doesn't work in Haskell tbh. Possibilities are that
(+)
is not recognized as integer addition. Or that the negative sign on-1
is parsed as a function.Indeed, Haskell is picky.
map (+ (-1)) [-10..0]
is a working solution.
Also there is another one, specifically to address the problem with the minus sign:
map (subtract 1) [-10..0]
Ah. I wonder why -1 requires parens. I also thought that you can turn an inline operator into a regular function by putting parens around it. (And vice versa with back ticks). I guess I should do my own Haskell homework instead of asking here. :)
If I remember accurately it is because anything that is not a value in haskell is a function.
So without the parentheses it can't know if
-1
means the function-
applied to some other value and the number one like1-1
( function-
applied to 1 and 1 ) or the value -1, so the way they solved it is to have it be the function, therefore you would be missing an argument or you would be passning a wrong type.When you want a value you would have to use
(-1)
so then it knows that you trully mean the value -1.PS: This is my first comment I hope I didn't come out sounding a but too "know it all" or trying to sound smart, I just wanted to explain it the best I can.