Вопрос

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!

Это было полезно?

Решение 2

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

Другие советы

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top