我必须创建一个包含两个数字(n和k)并返回其二项式系数的递归函数。
我必须使用pascal :: Int-> Int-> Int
老实说,我不明白哪里出了问题,谢谢您的帮助!
pascal :: Int -> Int -> Int
pascal n k
| k == 0 = 1
| n == n = 1
| k > n = 0
| otherwise = pascal(n-1)(k-1) + pascal(n-1) + k
以下错误是:
main.hs:7:40: error:
• Couldn't match expected type ‘Int’ with actual type ‘Int -> Int’
• Probable cause: ‘pascal’ is applied to too few arguments
In the second argument of ‘(+)’, namely ‘pascal (n - 1)’
In the first argument of ‘(+)’, namely
‘pascal (n - 1) (k - 1) + pascal (n - 1)’
In the expression: pascal (n - 1) (k - 1) + pascal (n - 1) + k
|
7 | | otherwise = pascal(n-1)(k-1) + pascal(n-1) + k
| ^^^^^^^^^^^
<interactive>:3:1: error:
• Variable not in scope: main
• Perhaps you meant ‘min’ (imported from Prelude)
答案 0 :(得分:5)
正如异常所言,pascal (n-1)
并没有多大意义,因为这是一个函数Int -> Int
,所以不能将pascal (n-1) (k-1)
和pascal (n-1)
一起添加。
因此,您需要传递一个额外的参数。例如:
pascal :: Int -> Int -> Int
pascal _ 0 = 1
pascal n k
| k == n = 1
| k > n = 0
| otherwise = pascal (n-1) (k-1) + pascal (n-1) k
请注意条件n == n
始终为真,您可能想说n == k
?