我有以下功能:
tempFunc :: Int-> Int-> Int
tempFunc x y
| y == 0 = 0
| x `mod` y == 0 = y + tempFunc x (y-1)
| otherwise = y-1
Run Code Online (Sandbox Code Playgroud)
其目的是递归地将所有数字因子相加.我想消除对第二个参数的需要y(因为y它等于x),所以我以下面的方式实现了这个功能
tempFunc :: Int-> Int-> Int
sumFactor num = tempFunc num num
where
tempFunc x y
...
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
The type signature for ‘tempFunc’ lacks an accompanying binding
Run Code Online (Sandbox Code Playgroud)
我注意到,当类型定义不正确时会出现这种类型的错误.但我无法弄清楚我的类型定义有什么问题,因为第一个摘录有效.
Tik*_*vis 11
函数的类型签名必须与函数本身在同一范围内.如果要在where子句中的函数中添加类型签名(通常不会这样做,但有时会有意义),您必须将它放在where子句本身中:
sumFactor num = tempFunc num num
where
tempFunc :: Int-> Int-> Int
tempFunc x y
| y == 0 = 0
| x `mod` y == 0 = y + tempFunc x (y-1)
| otherwise = y-1
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
200 次 |
| 最近记录: |