在Haskell中,在'where'语句内使用守卫定义一个函数

26

我刚开始自学Haskell。这段代码旨在完成质因数分解:

divides :: Integer -> Integer -> Bool
divides small big = (big `mod` small == 0)

lowestDivisor :: Integer -> Integer
lowestDivisor n = lowestDivisorHelper 2 n
    where lowestDivisorHelper m n
        | (m `divides` n) = m  -- these should belong to lowestDivisorHelper
        | otherwise = lowestDivisorHelper (m+1) n

primeFactors :: Integer -> [Integer]
primeFactors 1 = []
primeFactors n
    | n < 1 = error "Must be positive"
    | otherwise = let m = lowestDivisor n
                  in m:primeFactors (n/m)

在有注释的那一行我遇到了解析错误。我认为我的问题可能是因为lowestDivisorHelper有守卫条件,但编译器不知道这些守卫条件是属于lowestDivisorHelper还是lowestDivisor。我该如何解决这个问题?

我应该补充说明,为了隐藏实现细节,我不想将辅助函数定义在顶层。导入文件时不应该将辅助函数一起带过去。


3
导入文件时不应该将辅助函数一并导入。在这种情况下,只需不导出它即可。 - Alexey Romanov
1个回答

30
lowestDivisor :: Integer -> Integer
lowestDivisor n = lowestDivisorHelper 2 n where 
  lowestDivisorHelper m n
        | (m `divides` n) = m  -- these should belong to lowestDivisorHelper
        | otherwise = lowestDivisorHelper (m+1) n

你需要使用助手函数开启一个新语句,以便卫语句可以通过比较得到充分的缩进。 (而且你还忘记了一个参数,n。) 这种方法也可以:

lowestDivisor :: Integer -> Integer
lowestDivisor n = lowestDivisorHelper 2 n 
    where 
  lowestDivisorHelper m n
        | (m `divides` n) = m  -- these should belong to lowestDivisorHelper
        | otherwise = lowestDivisorHelper (m+1) n

但这个不行:

lowestDivisor :: Integer -> Integer
lowestDivisor n = lowestDivisorHelper 2 n 
  where lowestDivisorHelper m n
        | (m `divides` n) = m  -- these should belong to lowestDivisorHelper
        | otherwise = lowestDivisorHelper (m+1) n
关键点是竖线|必须在函数名的右侧更远处。
通常情况下,如果新的一行比前一行更靠右,则会将其视为继续上一行。保护符必须接在函数名后面。

啊,我刚想写一条评论说你最后的例子很好。 - Vitus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接