我已经尝试了无数谷歌搜索的答案,但是对哈斯克尔来说很新,我不明白我找到的一半东西,另一半只是不太相关。Haskell不能推断类型?
我的问题是这样的,如果我跑了ghci这些语句
Prelude> let x = 5 :: (Num a) => a
Prelude> sqrt x
我得到了我期望
2.23606797749979
但是,如果我把这个文件和编译(当然是我“M这里做的是相当琐碎)
sqrtNum :: (Num a, Floating b) => a -> b
sqrtNum x = sqrt x
我得到这个
myfile.hs:2:18:
Could not deduce (a ~ b)
from the context (Num a, Floating b)
bound by the type signature for
sqrtNum :: (Num a, Floating b) => a -> b
at test2.hs:1:12-40
`a' is a rigid type variable bound by
the type signature for sqrtNum :: (Num a, Floating b) => a -> b
at test2.hs:1:12
`b' is a rigid type variable bound by
the type signature for sqrtNum :: (Num a, Floating b) => a -> b
at test2.hs:1:12
Relevant bindings include
x :: a (bound at test2.hs:2:9)
sqrtNum :: a -> b (bound at test2.hs:2:1)
In the first argument of `sqrt', namely `x'
In the expression: sqrt x
该问题可能非常简单,我只是监督它(因为这是我遇到的每一个其他错误的经验),但这只是不点击。
在此先感谢!
您的类型声明可以将一种类型的数字“a”转换为另一种类型的“b”。但'sqrt'不这样做。 –