【发布时间】:2012-09-14 10:35:43
【问题描述】:
以下代码产生错误:
power:: Int -> Int -> Int
power a b
| a ==0 || b == 0 = 0
| otherwise = power ((multiply a a) (b-1))
multiply:: Int -> Int -> Int
multiply a b
| a <= 0 = 0
| otherwise = (multiply (a-1) (b)) + b
返回的错误是
power.hs:6:25:
Couldn't match expected type `Int' with actual type `Int -> Int'
In the return type of a call of `power'
Probable cause: `power' is applied to too few arguments
In the expression: power (multiply (a a) b - 1)
In an equation for `power':
power a b
| b == 0 = 0
| otherwise = power (multiply (a a) b - 1)
【问题讨论】:
-
该错误与该代码不匹配。太不礼貌了!
-
如果您要代表数学乘方和乘法,则需要更改定义。目前,幂 2 0 == 0,而 2^0=1。事实上,正因为如此,幂永远只会是 0。对于其他数字,每次都是幂平方,所以一旦你解决了 0 的问题,你就会得到幂 x y 计算 x^(2^y)。您需要跟踪起始数字 y 才能停止此操作。
标签: haskell