gpt4 book ai didi

haskell - 无法将预期类型 Int 与 Haskell 中的实际类型 Int -> Int 匹配

转载 作者:行者123 更新时间:2023-12-04 18:11:34 26 4
gpt4 key购买 nike

以下代码会产生错误:

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)

最佳答案

错误在表达式 power ((multiply a a) (b-1)) 中.问题是额外的一对括号。您实际上只将一个参数传递给 power ,即 ((multiply a a) (b-1)) .这个表达式本身是无效的,因为 (multiply a a) 的结果是 Int ,它不能接受参数。

您应该将其重写为

| otherwise   = power (multiply a a) (b-1)

关于haskell - 无法将预期类型 Int 与 Haskell 中的实际类型 Int -> Int 匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12540105/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com