2015-01-20 62 views
2

我有以下功能:哈斯克尔 - “没有一个伴随的结合”为递归函数

tempFunc :: Int-> Int-> Int 
tempFunc x y 
    | y == 0 = 0 
    | x `mod` y == 0 = y + tempFunc x (y-1) 
    | otherwise = y-1 

其目的是一个号码的所有因素递归加在一起。我想消除第二个参数y需要(因为y等于x),所以我以下列方式

tempFunc :: Int-> Int-> Int 
sumFactor num = tempFunc num num 
    where 
     tempFunc x y 
     ... 

实现的功能,但我得到了以下错误:

The type signature for ‘tempFunc’ lacks an accompanying binding 

我注意到,这种类型的错误出现在类型定义不正确时。但是我无法弄清楚我的类型定义有什么问题,因为第一个摘录是有效的。

+0

遭到举报一次之前,我希望人们看到我编辑了我的问题。我确信现在符合指导原则。 – Bolboa 2015-01-21 20:13:48

回答

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 
+0

@Bolboa:好的,做出了改变 – 2015-01-20 19:00:21