No instance for (Fractional Int) arising from a use of `leap'
Possible fix: add an instance declaration for (Fractional Int)
In the first argument of `(==)', namely `leap (x)'
In the second argument of `(&&)', namely `leap (x) == 1'
In the expression: (y == 2) && leap (x) == 1在ghci中加载文件时获取此错误
这是导致错误的函数
daysInMonth :: Int -> Int -> Int
daysInMonth y x
| (y == 1 || y == 3 || y == 5 || y == 7 || y == 8 || y == 10 || y == 12) = 31
| (y == 4 || y == 6 || y == 9 || y == 11) = 30
| (y == 2) && leap(x) == 1 = 28
| (y == 2) && leap(x) == 0 = 29
where
leap a = if (a / 4) == 0 then return 1 else return 0发布于 2013-10-28 21:04:07
您正在计算a / 4,其中a (它只等于x,Int)是Int。Int类型不属于Fractional类型,因此没有为Int定义/。
a将Fractional类型转换为if fromIntegral a / 4 == 0 then ...类型if quot a 4 == 0 then ...另外,Haskell中的return与其他语言中的return不同。在这里把它处理掉。
(也是这不是你决定闰年的方法.)
https://stackoverflow.com/questions/19644968
复制相似问题