Skip to content

re: Little partial application challenge in Haskell VIEW POST

re: Personally, I think the lambda solution is perfect without partial application because it is easy to read. But for the purposes of this challenge, ...

It works, I checked. That's a pretty clever solution.


Doh, I missed the more obvious solution. 🙃

map ((+) -1) [-10..0]

Very close.

This one gives:

<interactive>:1:1: error:
     Non type-variable argument in the constraint: Num (a -> a -> a)
      (Use FlexibleContexts to permit this)
     When checking the inferred type
        it :: forall a. (Num (a -> a -> a), Num a, Enum a) => [a -> a]

Huh, this works verbatim in F#. ((+) -1) [-10..0]

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.

code of conduct - report abuse