let abs x = if x < 0 then -x else x Prel...">

"No instance for" error

Following the example of 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?

+6
source share
2 answers

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

 abs (-3) 
+14
source

The interpreter believes that you mean abs - 3 not abs (-3) . You need brackets to fix code errors and make sure you understand that you are going to use the unary "-" function, not the subtraction operator.

+5
source

Source: https://habr.com/ru/post/889676/


All Articles