user3377437
user3377437

Reputation: 183

Haskell - type classes

I am new to Haskell, and am trying to learn how type classes work. I typed the following code into my GHCi compiler.

let (+) :: Num a => a -> a -> a;
(+) a b = a+b;

The code compiles, but whenever I call the function, it stuck and I have to ctrl+c to stop the process.

Am I doing anything wrong here? Thank you in advance!

Upvotes: 2

Views: 167

Answers (2)

Ben
Ben

Reputation: 71400

You've defined a + b to be equal to a + b (the + infix operator can also be written as (+), in which case it behaves as an ordinary prefix function; but your left-hand-side is still the same thing as your right-hand-side).

So the interpreter is just spinning forever, as to evaluate a + b it then needs to evaluate a + b, which then requires an evaluation of a + b, and so on.

Upvotes: 8

bereal
bereal

Reputation: 34252

You are calling your function + from your function + recursively, it's the same as if you wrote:

add :: Num a => a -> a -> a
add a b = add a b

Upvotes: 7

Related Questions