简体   繁体   English

我该如何修复这个 function

[英]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.我一直在使用 Haskell 完成一项学校作业,并且一遍又一遍地遇到相同的解析错误。 I've been looking online and have gotten no solutions to my problem.我一直在网上寻找并没有解决我的问题。 Any advice would be greatly appreciated.任何建议将不胜感激。 Here is the function...这里是 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 |这有两个问题,缺少 pipe 字符| on the guards, and you forgot to include a where keyword before the definition of compareMax .在警卫上,您忘记在compareMax的定义之前包含where关键字。

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您最初随问题发布的代码也是通过将帮助程序 function 重写为顶级 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)

This implementation is also slightly different in that it handles empty lists instead of erroring.此实现也略有不同,因为它处理空列表而不是错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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