Haskell - defining a function with guards inside a 'where'

这一生的挚爱 提交于 2019-12-21 07:08:25

问题


I'm just starting out at teaching myself Haskell. This code is supposed to do prime factorisation:

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)

I get a parse error on the commented line. I think my problem might be that lowestDivisorHelper has guards, but the compiler doesn't know whether the guards belong to lowestDivisorHelper or lowestDivisor. How do I get around this?

I should add that I didn't want to define the helper function at the top level in order to hide the implementation detail. Importing the file shouldn't take the helper function with it.


回答1:


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

You need to start a new statement with your helper function for the guards to be sufficiently indented by comparison. (And you also forgot an argument, n.) This would also work:

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

but this doesn't:

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

The key point is that the | has to be further to the right than the function name.

In general, starting a new line continues the previous one as long as it is further to the right. The guards have to continue on from the function name.



来源:https://stackoverflow.com/questions/13327374/haskell-defining-a-function-with-guards-inside-a-where

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!