我写了一个函数,该函数可以计算加泰罗尼亚语的数字,但不能编译。我收到此错误消息:
let cb = (v) => `<div class="${v[0]}"><label for="${v[1][0]['tabLabel']}"><i class="${v[1][0]['tabIcon']}"></i></label></div>` Object.entries(TabFormData['EN']).map(cb);
这是我的代码:
• Occurs check: cannot construct the infinite type: t ~ t -> t
Relevant bindings include
catalanNum :: t (bound at CatNum.hs:1:1)
如何消除此错误?
答案 0 :(得分:8)
catalanNum 0 = 1 catalanNum n = div(2 *(catalanNum * (n-1)) * (2 * (n-1) + 1))(n + 2)
The problem in your code is here,
catalanNum * (n-1)
^
You are multiplying a function with an integer. You probably want to write,
catalanNum (n-1)
More importantly though, you should know how to find such errors. And even if you do not understand the error message, one thing should do is to trim down the problematic expression. The original expression is,
catalanNum n = div (2 *(catalanNum * (n-1)) * (2 * (n-1) + 1)) (n + 2)
The div
takes two arguments, in principle one of the is problematic, and it's likely to be the first since the second is rather simple. So trim it down.
catalanNum n = 2 *(catalanNum * (n-1)) * (2 * (n-1) + 1)
You can see this as three factors being multiplied,
2
catalanNum * (n-1)
2 * (n-1) + 1
Look at them. If you don't see the error try them out. The first is irrelevant. The last works,
catalanNum n = 2 * (n-1) + 1
The middle one doesn't,
catalanNum n = catalanNum * (n-1)
This should now be relatively obvious.
After trimming your expression, if you still can't see the source of the error, add a type signature catalanNum :: Int -> Int
. The error then becomes,
Couldn't match expected type ‘Int’ with actual type ‘Int -> Int’
You'd expect catalanNum
to be an Int
in order to multiply it, but it's of type Int -> Int
.
Regarding the original error,
Occurs check: cannot construct the infinite type: t ~ t -> t
Without a type signature the type inference algorithm arrives at the conclusion that you have an expression whose type must be both, some t
and t->t
(written t ~ t->t
). But then it would have infinite length and depth, since you'd also conclude,
t ~ t->t
t ~ t->t->t
t ~ t->t->t->t
...
and
t ~ t->t
t ~ (t->t)->t
t ~ ((t->t)->t)->t
...
and combinations of both.