【发布时间】:2022-01-15 19:36:56
【问题描述】:
不幸的是,我遇到了一个奇怪的错误。使用带有数据构造函数的中缀时会发生这种情况。
我是 Haskell 的新手。有人可以在这方面帮助我吗?
Prelude> data L a = Cons a (L a) | Emp deriving Show
Prelude> 10 `Cons` Emp
Cons 10 Emp
Prelude> 10 `Cons` 10 `Cons` Emp
<interactive>:43:1: error:
• Non type-variable argument in the constraint: Num (L a)
(Use FlexibleContexts to permit this)
• When checking the inferred type
it :: forall a. (Num a, Num (L a)) => L (L a)
Prelude> 10 `Cons` (10 `Cons` Emp)
Cons 10 (Cons 10 Emp)
Prelude> 10 `Cons` 10 `Cons` Emp
<interactive>:45:1: error:
• Non type-variable argument in the constraint: Num (L a)
(Use FlexibleContexts to permit this)
• When checking the inferred type
it :: forall a. (Num a, Num (L a)) => L (L a)
Prelude> data L a = Emp | Cons a (L a) deriving Show
Prelude> 10 `Cons` 10 `Cons` Emp
<interactive>:47:1: error:
• Non type-variable argument in the constraint: Num (L a)
(Use FlexibleContexts to permit this)
• When checking the inferred type
it :: forall a. (Num a, Num (L a)) => L (L a)
Prelude>
【问题讨论】:
-
如您所见,问题在于关联性。即使它不是完全重复的,这里的答案也完美地解释了这种行为以及如何解决它:stackoverflow.com/questions/8139066/…
-
Cons需要是右关联运算符。