Haskell - "lacks an accompanying binding" 用于递归函数

Haskell - "lacks an accompanying binding" for recursive function

我有以下功能:

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

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

函数的类型签名必须与函数本身在同一范围内。如果你想在 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