我有
coefficient :: ???????
coefficient = 1.0
和
val :: Int
我想做
result :: ???????
result val coefficient = val * coefficient
我需要做哪些类型签名和转换功能才能完成这项工作?如果我想有能力将 val 推广到任何类型的 Num,我必须做什么?
这个:
coefficient = 1.0
val :: Int
val = 3
result :: Num a => a
result = coefficient * (fromIntegral val)
给我这个编译器警告:
Could not deduce (a ~ Double)
from the context (Num a)
bound by the type signature for result :: Num a => a
at Move.hs:17:1-41
`a' is a rigid type variable bound by
the type signature for result :: Num a => a at Move.hs:17:1
In the first argument of `(*)', namely `coefficient'
In the expression: coefficient * (fromIntegral val)
In an equation for `result':
result = coefficient * (fromIntegral val)
我知道那不是我最初问的,我在清理代码时犯了一些错误。
现在有一个系数类型:
coefficient :: Num a => a
coefficient = 1.0
val :: Int
val = 3
result :: Num a => a
result = coefficient * (fromIntegral val)
产生的错误:
Could not deduce (Fractional a) arising from the literal `1.0'
from the context (Num a)
bound by the type signature for coefficient :: Num a => a
at Move.hs:12:1-17
Possible fix:
add (Fractional a) to the context of
the type signature for coefficient :: Num a => a
In the expression: 1.0
In an equation for `coefficient': coefficient = 1.0