我已經嘗試了無數谷歌搜索的答案,但是對哈斯克爾來說很新,我不明白我找到的一半東西,另一半隻是不太相關。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'不這樣做。 –