Reputation: 69
*Main> (negate.abs) -7
<interactive>:26:1: error:
? Non type-variable argument in the constraint: Num (c -> c)
(Use FlexibleContexts to permit this)
? When checking the inferred type
it :: forall c. (Num (c -> c), Num c) => c -> c
Hi, I am the questioner of Some errors and difficulties with Haskell function composition, and now I got another trouble. Why this simple code not works? Here is not-working code lists:
(negate.abs) -7
negate (abs -7)
negate.abs -7
negate.abs $ 7 //only this works
with all same error message above. I couldn't figure it out, so could I get a little help about why it doesn't work and how can I fix it? And also I'd like to understand the meaning of the error message, and why only the last one works. Thank you.
Upvotes: 1
Views: 127
Reputation: 476574
If we look at the expression:
(negate.abs) -7
we see actually a subtraction. Indeed: we subtract 7
from (negate . abs)
, so for Haskell it looks like you wrote:
(negate.abs) - (7)
Now in case you perform a subtraction, both operands need to be a Num
(and both parameters need to have the same type, but that is not relevant here), since (-)
has type (-) :: Num n => n -> n -> n
.
The other expressions face the same problem (although of course with different details).
The way to solve it is to let Haskell understand that the -
is an unary minus that is related to 7
, we can do this for instance by using brackets:
(negate.abs) (-7)
But we could have isolated it in other ways as well, for instance with a let
statement:
let minusseven = -7 in (negate.abs) minusseven
Upvotes: 9