我正在尝试编写一个简单的函数来删除数字的最后一位数字,并返回数字的其余部分。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。泽塔击败你,所以他得到了接受。 –