2
以下類型簽名的幅度找表示爲一個元組向量的大小似乎不工作:函數的類型簽名來找到一個向量
mag :: (Floating b, Num a) => (a,a) -> b
mag (x,y) = sqrt (x**2 + y**2)
錯誤:
計算一個類型簽名Couldn't match expected type `b' against inferred type `a'
`b' is a rigid type variable bound by
the type signature for `mag' at tone.hs:1:17
`a' is a rigid type variable bound by
the type signature for `mag' at tone.hs:1:24
In the expression: sqrt (x ** 2 + y ** 2)
In the definition of `mag': mag (x, y) = sqrt (x ** 2 + y ** 2)
是的,我的意思是有一個轉換那裏。並且,謝謝,fromIntegral像魅力一樣工作(沒有截斷)。 – Manu 2011-04-16 17:36:35
要求是對兩個積分數有浮點數。 fromIntegral和(Floating b,Integral a)的使用有效。 – Manu 2011-04-16 17:44:23
風格點:大多數Haskell不會像其他語言那樣在括號中放置參數;這通常具有類型「mag :: a - > a - > a」。我可以看到,在這裏你可能想把一個向量看作一個(x,y)對,所以一個元組是有意義的。但是在這種情況下,您應該使用類型同義詞來記錄它:「type Vector a =(a,a)」 – 2011-04-16 19:53:18