我試圖編寫一個函數,測試一個數字的平方根是否是整數。 我的函數如下:Haskell沒有實例...出現
check :: [Int] -> [Int]
check xs = filter (\n -> n == round sqrt n) xs
而且我收到錯誤消息是這些:
No instance for (Integral (Int -> Int))
(maybe you haven't applied enough arguments to a function?)
arising from a use of "round"
In the second argument of "(==)", namely "round sqrt n"
...
No instance for (Floating a0) arising from a use of `sqrt'
The type variable `a0' is ambiguous
Note: there are several potential instances:
instance Floating Double -- Defined in `GHC.Float'
instance Floating Float -- Defined in `GHC.Float'
In the first argument of `round', namely `sqrt'
編輯:我結束了重寫整個問題,我希望那不是問題!
你想'round(sqrt n)'而不是'round sqrt n'。你還需要一個fromIntegral的右手錶達。因此'check = filter(\ n - > n == fromIntegral(round(sqrt n)))' – pdexter
函數調用是不是右對應的? 此外,我仍然收到兩條錯誤消息: '沒有使用「round」引起的(RealFrac Int)實例 '沒有使用「sqrt」引發的(Floating Int)實例 – DQQpy
刪除您的' [Int] - > [Int]'類型,例如,您想要'[Double] - > [Double]'。如果你真的想要'Int',那麼你必須進一步改變定義。函數調用是左關聯的。 – pdexter