所以我得到了错误:
error:
parse error on input `|'
| x == 'a' = True
代码本身是:
module SubstitutionCiphers where
import AssignmentHelp.Cipher
validateCipher :: Cipher -> Boolean
validateCipher "" = False
validateCipher (x:xs)
| x == 'a' = True
| otherwise = validateCipher xs
我尝试以各种不同的方式交替使用缩进,但似乎没有任何效果。我想这仍然是一个缩进问题,但是我不知道如何解决它。
答案 0 :(得分:6)
缩进定义的第一行时,解析器(在这种情况下)认为您正在继续前一行,就好像您已经写过
validateCipher :: Cipher -> Bool validateCipher "" = False -- etc
通过更简单的定义,这一点变得显而易见
validateCipher :: String -> Bool
validateCipher _ = True
因为解析器能够接受它。但是,类型签名的结果放置无效。
<interactive>:11:1: error:
Illegal type signature: ‘String -> Bool validateCipher _’
Type signatures are only allowed in patterns with ScopedTypeVariables
(我怀疑,如果ScopedTypeVariables
扩展名不存在,您将在=
上遇到更通用的解析器错误。)
在您的实际定义中,解析器将继续尝试解析类型签名,并且直到遇到|
时才遇到 syntactic 问题。
解决方案是不缩进定义:
validateCipher :: Cipher -> Bool
validateCipher "" = False
validateCipher (x:xs)
| x == 'a' = True
| otherwise = validateCipher xs
答案 1 :(得分:4)
您不应缩进签名下方的部分。只需将其写为:
validateCipher :: Cipher -> Bool
validateCipher "" = False
validateCipher (x:xs)
| x == 'a' = True
| otherwise = validateCipher xs
或者您可以通过以下方式进行检查:
validateCipher :: Cipher -> Bool
validateCipher = elem 'a'