[英]Haskell - type classes
I am new to Haskell, and am trying to learn how type classes work. 我是Haskell的新手,我正在尝试学习类型类的工作原理。 I typed the following code into my GHCi compiler. 我在GHCi编译器中输入以下代码。
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. 代码编译,但每当我调用该函数时,它就会卡住,我必须按ctrl + c来停止进程。
Am I doing anything wrong here? 我在这里做错了吗? Thank you in advance! 先感谢您!
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). 你已经将a + b
定义为等于a + b
( +
缀运算符也可以写成(+)
,在这种情况下它表现为普通的前缀函数;但你的左侧仍然是相同的作为右手边的东西)。
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. 所以解释器只是永远旋转,为了评估a + b
然后需要评估a + b
,然后需要评估a + b
,依此类推。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.