Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"No instance for" error

Tags:

haskell

ghci

Following an example in http://en.wikibooks.org/wiki/Haskell/Beginning

Prelude> let abs x = if x < 0 then -x else x
Prelude> abs 5
5
Prelude> abs -3

<interactive>:1:6:
    No instance for (Num (a0 -> a0))
      arising from the literal `3'
    Possible fix: add an instance declaration for (Num (a0 -> a0))
    In the second argument of `(-)', namely `3'
    In the expression: abs - 3
    In an equation for `it': it = abs - 3

What's wrong?

like image 610
zaf Avatar asked Jun 03 '11 07:06

zaf


2 Answers

Haskell thinks you're trying to subtract 3 from abs, and is complaining that abs is not a number. You need to add parenthesis when using the unary negation operator:

abs (-3)
like image 81
hammar Avatar answered Nov 07 '22 07:11

hammar


The interpreter thinks you mean abs - 3 not abs (-3). You need brackets to disambiguate the code and make sure it's clear that you intend to use the unary "-" function, not the subtraction operator.

like image 5
MGwynne Avatar answered Nov 07 '22 09:11

MGwynne