我正在嘗試編寫一個簡單的函數來刪除數字的最後一位數字,並返回數字的其餘部分。Haskell中的瑣碎數字轉換問題
dropLastDigit :: (Integral b) => b -> b
dropLastDigit x = (quot x 10) * (floor $ logBase 10 x)
然而,當我嘗試這個加載到ghci中,我得到:
Could not deduce (Floating b) arising from a use of ‘logBase’
from the context (Integral b)
bound by the type signature for
dropLastDigit :: Integral b => b -> b
at haskelljokes.hs:6:18-39
Possible fix:
add (Floating b) to the context of
the type signature for dropLastDigit :: Integral b => b -> b
In the second argument of ‘($)’, namely ‘logBase 10 x’
In the expression: floor $ logBase 10 x
In an equation for ‘dropLastDigit’:
dropLastDigit x = floor $ logBase 10 x
然而,在ghci中運行此代碼:
:t (quot 101 10) * (floor $ logBase 10 101)
生產:(quot 101 10) * (floor $ logBase 10 101) :: Integral a => a
我的問題是,我做錯了什麼?爲什麼(相同的代碼?)在ghci中工作?
謝謝你明確的迴應。真的有助於提醒我思考我的類型類。 Upvoted。澤塔擊敗你,所以他得到了接受。 –