Haskell 关系错误 - 声明中的语法错误(意外的“;',可能是由于布局错误)

发布于 2024-11-01 15:00:59 字数 615 浏览 0 评论 0原文

我对 Haskell 相当陌生,我不完全理解这个错误,当我加载文件时,拥抱在“检查”行打印出以下“声明中的语法错误(意外的`;',可能是由于布局错误)” s1 s2 ((x,y):xs)”。我觉得这很令人困惑,因为没有“;”在代码中。如果有人能解释为什么会发生这种情况以及如何解决它,我将非常感激。下面是我的代码。

type Owned = String  
type Owner = String  
type Fact = (Owned,Owner)

database = [(String, String)]  
database = [("c4","c5"),("c1","c2"), ("c2", "c3"), ("c3","c4")]

owns :: Owner -> Owned -> Bool

owns s1 s2  
      | check s1 s2 database = true  
      | otherwise false

check s1 s2 ((x,y):xs)  
     | s1==x && y==s2 = true  
     | s1==x && y==s2 = (check y s2 database)  
     | otherwise false

I'm fairly new to Haskell and I don't fully understand this error, when I load the file hugs prints out the following "Syntax error in declaration (unexpected `;', possibly due to bad layout)" at the line "check s1 s2 ((x,y):xs)". I find this confusing as there isn't a ";" in the code. If someone could explain why this is happening and how I can fix it I would be very grateful. Bellow is my code.

type Owned = String  
type Owner = String  
type Fact = (Owned,Owner)

database = [(String, String)]  
database = [("c4","c5"),("c1","c2"), ("c2", "c3"), ("c3","c4")]

owns :: Owner -> Owned -> Bool

owns s1 s2  
      | check s1 s2 database = true  
      | otherwise false

check s1 s2 ((x,y):xs)  
     | s1==x && y==s2 = true  
     | s1==x && y==s2 = (check y s2 database)  
     | otherwise false

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼眸 2024-11-08 15:00:59

您在 otherwise 分支中缺少 =

type Owned = String  
type Owner = String  
type Fact = (Owned,Owner)

database = [(String, String)]  
database = [("c4","c5"),("c1","c2"), ("c2", "c3"), ("c3","c4")]

owns :: Owner -> Owned -> Bool

owns s1 s2  
      | check s1 s2 database = true  
      | otherwise = false

check s1 s2 ((x,y):xs)  
     | s1==x && y==s2 = true  
     | s1==x && y==s2 = (check y s2 database)  
     | otherwise = false

You are missing an = in the otherwise branches:

type Owned = String  
type Owner = String  
type Fact = (Owned,Owner)

database = [(String, String)]  
database = [("c4","c5"),("c1","c2"), ("c2", "c3"), ("c3","c4")]

owns :: Owner -> Owned -> Bool

owns s1 s2  
      | check s1 s2 database = true  
      | otherwise = false

check s1 s2 ((x,y):xs)  
     | s1==x && y==s2 = true  
     | s1==x && y==s2 = (check y s2 database)  
     | otherwise = false
说不完的你爱 2024-11-08 15:00:59

事实上,有;在转换后的源代码中。 Haskell 报告包含有关如何使用布局规则转换源代码的详细说明。人们应该读一遍它们,它非常直观。

尽管此类错误确实会让新手感到困惑,但以下经验法则适用:

  • 如果编译器抱怨“;”在报告的行之前的最后一个非空行中存在语法错误。
  • 通常,错误是由于布局引起的(实际上正如编译器所建议的那样)。尽管如此,还有其他情况,如您的示例所示:编译器拼命在函数 owns 的第二个保护中查找“=”,并且当他在 check 之前找到分号时他知道出了什么问题。

Actually, there are ; in the transformed source code. The Haskell Report contains a detailed explanation about how the source code is transformed with the layout rules. One should read them once, it's quite intuitive.

Though such errors are indeed confusing for a newcomer, the following rules of thumb apply:

  • If the compiler complains about ';' you have a syntax error in the last nonempty line before the line reported.
  • More often than not, the error comes about due to layout (as indeed the compiler suggested). Nevertheless, there are other cases as your example shows: the compiler desperately looks for a '=' in the second guard of function owns and when he finds the semicolon just before check he knows there is something wrong.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文