由于使用'it'而导致(小数a0) [英] No instance for (Fractional a0) arising from a use of ‘it’
本文介绍了由于使用'it'而导致(小数a0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Prelude> 有人知道为什么这段代码在GHCI中失败吗?令x = 4总和[(x ^ pow)/ product [1.. pow] | pow [ - [0..9]]
< interactive>:70:1:
由于使用'it'而导致的(小数a0)没有实例
类型变量'a0'不明确
解决方案
div:
Prelude>令x = 4总和[(x ^ pow)`div` product [1.. pow] | pow < - [0..9]]
50
注意运营商:
Prelude> :t(/)
(/)::小数a => a - > a - > a
前奏> :t div
div :: Integral a => a - > a - > a
/
用于小数<$如果你需要使用浮点运算符(**)而不是(^):
Prelude>令x = 4总和[(x ** pow)/ product [1.. pow] | pow < - [0..9]]
54.15414462081129
再次注意类型:
Prelude> :t(^)
(^)::(积分b,数字a)=> a - > b - > a
前奏> :t(**)
(**):: Floating a => a - > a - > a
Does anyone know why this code fails in GHCI?
Prelude> let x = 4 in sum [(x^pow) / product[1.. pow] | pow <- [0.. 9]]
<interactive>:70:1:
No instance for (Fractional a0) arising from a use of ‘it’
The type variable ‘a0’ is ambiguous
解决方案
Just use div:
Prelude> let x = 4 in sum [(x^pow) `div` product[1.. pow] | pow <- [0.. 9]]
50
Notice the types of the operators:
Prelude> :t (/)
(/) :: Fractional a => a -> a -> a
Prelude> :t div
div :: Integral a => a -> a -> a
/
is for fractional numbers div
for integrals ones
If you need floating point result use (**) operator instead of (^):
Prelude> let x = 4 in sum [(x**pow) / product[1.. pow] | pow <- [0.. 9]]
54.15414462081129
Mind the types once more:
Prelude> :t (^)
(^) :: (Integral b, Num a) => a -> b -> a
Prelude> :t (**)
(**) :: Floating a => a -> a -> a
这篇关于由于使用'it'而导致(小数a0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文