user8314628
user8314628

Reputation: 2042

How can I negate a user defined Integer?

I have

data Weight = Fin Integer | Inf
    deriving (Eq, Ord, Show)

negate :: Weight -> Weight
negate Inf = error "negative infinity not supported"

I want negate Fin (-1) = Fin 1. So I further define

negate Fin x = Fin (0 - x)

But this gives error

? Equations for ‘negate’ have different numbers of arguments

How can I fix it? Thank you!

Upvotes: 0

Views: 354

Answers (1)

AJF
AJF

Reputation: 11923

You need brackets for pattern matches:

negate (Fin x) = Fin (0 - x)

Otherwise, it looks like you've got two arguments.

This is reflected in the error: "Equations for ‘negate’ have different numbers of arguments".

This doesn't apply for Inf, because it takes no arguments.

Upvotes: 3

Related Questions