I wanted to define absolute values in Haskell, but I didn't really understand how to define them.
By the way, I defined it as follows, but what's wrong with you?
abs(x) = if x <0 then -x
else if 0<= x then x
(The absolute function is abs.)
haskell
The second if expression on the right side of the definition shown does not have an else clause, which results in a grammatical error.
Parse error (possibly incorrect indication or mismatched brackets)
(This error message is a little hard to understand.
If you want to define an absolute value function for a normal number, the if expression does not need to be in two stages.
abs x = if x <0 then-x else x
I think that's all right. Now, grammatically, this will pass, but when I try to use it.
*Main>abs(-3)
Ambiguous occurrence `abs'
...
Error like . This is an error message that the absolute value function abs for numeric type (type of instance of type class Num) is defined by default in the standard Prelude module and does not know which one to use. If the original purpose is to use an absolute value function, it should not be named.
© 2024 OneMinuteCode. All rights reserved.