下面的代码产生一个错误:无法与实际Int类型匹配期望的类型INT - >内部在Haskell
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来阻止它。 – AndrewC