所以,这里有两个列表推导,第一个使用^
,第二个使用**
:
> [x ^ 2 | x <- [1..10], odd x]
[1,9,25,49,81]
> [x ** 2 | x <- [1..10], odd x]
<interactive>:9:1:
No instance for (Show t0) arising from a use of ‘print’
The type variable ‘t0’ is ambiguous
Note: there are several potential instances:
instance Show Double -- Defined in ‘GHC.Float’
instance Show Float -- Defined in ‘GHC.Float’
instance (Integral a, Show a) => Show (GHC.Real.Ratio a)
-- Defined in ‘GHC.Real’
...plus 23 others
In a stmt of an interactive GHCi command: print it
据我所知,两个运算符之间的区别在于第一个使用整数,而第二个使用浮点值。然后是预期的产出:
[1.0,9.0,25.0,49.0,81.0]
实际问题是:为什么第二个列表理解失败?
答案 0 :(得分:3)
如你所说**
适用于浮动的ponints。但是odd
仅适用于Integral
。因此,您的第二个列表推导仅适用于Floating
和Integral
实例的类型,并且这种类型不存在。
但是,我不确定为什么错误消息声称有26个可能的实例,当它提到的实例都没有实际满足所需的约束时。