简体   繁体   中英

How can I fix this function

I've been working on a school assignment using Haskell and keep getting the same parse-errors over and over again. I've been looking online and have gotten no solutions to my problem. Any advice would be greatly appreciated. Here is the function...

maxGaussNorm :: [GaussianInt] -> GaussianInt
maxGaussNorm [] = (0,0)
maxGaussNorm gs = maxAux (tail gs) (head gs)

maxAux :: [GaussianInt] -> GaussianInt -> GaussianInt
maxAux gs m 
      | gs == [] = m
      | (gaussNorm (head gs)) <= (gaussNorm m) = maxAux (tail gs) m
      | (gaussNorm (head gs)) > (gaussNorm m) = maxAux (tail gs) (head gs)

Try it online!

The code you have posted in the link is different from what you have posted in the question body.

Here is your code that actually has parse errors

maxGaussNorm :: [GaussianInt] -> GaussianInt
maxGaussNorm gs = compareMax (tail gs) (head gs)
    compareMax :: [GaussianInt] -> GaussianInt -> GaussianInt
    compareMax gs m
        gs == [] = m
        gaussNorm(head gs) <= gaussNorm m = compareMax (tail gs) m
        gaussNorm(head gs) > gaussNorm m = compareMax (tail gs) (head gs)

There are two things wrong with this, missing the pipe character | on the guards, and you forgot to include a where keyword before the definition of compareMax .

Here is the code with these parse error fixed

maxGaussNorm :: [GaussianInt] -> GaussianInt
maxGaussNorm gs = compareMax (tail gs) (head gs)
  where
    compareMax :: [GaussianInt] -> GaussianInt -> GaussianInt
    compareMax gs m
       | gs == [] = m
       | gaussNorm(head gs) <= gaussNorm m = compareMax (tail gs) m
       | gaussNorm(head gs) > gaussNorm m = compareMax (tail gs) (head gs)

The code you originally posted with the question also is a solution to the parse errors by rewriting the helper function as a top level function and including the pipes on the guards

maxGaussNorm :: [GaussianInt] -> GaussianInt
maxGaussNorm [] = (0,0)
maxGaussNorm gs = maxAux (tail gs) (head gs)

maxAux :: [GaussianInt] -> GaussianInt -> GaussianInt
maxAux gs m 
      | gs == [] = m
      | (gaussNorm (head gs)) <= (gaussNorm m) = maxAux (tail gs) m
      | (gaussNorm (head gs)) > (gaussNorm m) = maxAux (tail gs) (head gs)

This implementation is also slightly different in that it handles empty lists instead of erroring.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM