我对Haskell相当新,我不完全理解这个错误,当我加载文件时,hugs打印出以下“声明中的语法错误(意外的`;',可能是由于布局错误)” line“check 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
答案 0 :(得分:4)
您在=
分支机构中遗漏了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
答案 1 :(得分:2)
实际上,有;在转换后的源代码中。 Haskell报告包含有关如何使用布局规则转换源代码的详细说明。一个人应该读一遍,这很直观。
虽然这些错误确实让新手感到困惑,但以下经验法则适用:
owns
的第二个保护中拼命寻找'=',当他在check
之前找到分号时,他知道存在错误