Haskell RealFrac错误

时间:2015-12-08 16:00:18

标签: haskell

错误:定义dayshoursmins所需的RealFrac Int实例

我理解这个错误是因为我正在改变类型 - 或者沿着那些行改变 - 并且它不匹配(RealFrac a,Integral b)=> a - >湾我只是不确定如何解决它。错误发生在第2行。我尝试过使用fromIntegral,但我可能使用不正确。

dayshoursmins :: Int -> (Int, Int, Int)
dayshoursmins x = (days, hours, minutes)
where
    days = floor (x / (24 * 60))
    a = x - (days * (24 * 60))
    hours = floor (a / 60)
    b = a - (hours * 60)
    minutes = b

1 个答案:

答案 0 :(得分:2)

/函数,如果是小数并返回Fractional

> :t (/)
(/) :: Fractional a => a -> a -> a 

如果要使用整数除法,请改用div函数:

dayshoursmins :: Int -> (Int, Int, Int)
dayshoursmins x = (days, hours, minutes)
    where
        days = x `div` (24 * 60)
        a = x - (days * (24 * 60))
        hours =  a `div` 60
        b = a - (hours * 60)
        minutes = b