Haskell 关系错误 - 声明中的语法错误(意外的“;',可能是由于布局错误)
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在
otherwise
分支中缺少=
:You are missing an
=
in theotherwise
branches:事实上,有;在转换后的源代码中。 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:
owns
and when he finds the semicolon just beforecheck
he knows there is something wrong.