输入`='?解析错误

时间:2017-11-21 14:36:27

标签: haskell syntax compiler-errors keyword

我是Haskell的新手,我试图通过使用移动ASCII值来加密纯文本,如果我的纯文本包含数字,那么它必须通过用特殊符号代替数字来编码每个数字(0 = *,1 =' \',2 =〜,3 =!,4 = @,5 =#,6 = $,7 =%,8 = ^,9 =&)以下是我的代码

import Data.Char

isInRange :: Char -> Char -> Char -> Bool
isInRange l u c = (c >= l) && (c <= u)

--is this Letter to be ciphered
canEncrypt :: Char -> Bool
canEncrypt c = isLower(c) && isAscii(c)

-should we wrap around the alphabet ?
wraparound shift c
 | isLower c && ord(c)+shift > ord 'z' = True
 | otherwise = False

--encrypt single char at a time
encryptChar :: Char -> Char -> Char
encryptChar :: Char -> Char -> Char
encryptChar shift c
  | canEncrypt c = chr (ord c + wrapshift)
  | isUpper c = c
  Where wrapshift = let shift' = ord(shift) `mod` 26
                    in if (wraparound shift' c)
                        then shift'-26
                        else shift'
encryptChar _ '0' = '*'
encryptChar _ '1' = '\''
encryptChar _ '2' = '~'
encryptChar _ '3' = '!'
encryptChar _ '4' = '@'
encryptChar _ '5' = '#'
encryptChar _ '6' = '$'
encryptChar _ '7' = '%'
encryptChar _ '8' = '^'
encryptChar _ '9' = '&'
encryptChar _ c = c

encryptText :: String -> String -> String
encryptText text xs = [ encryptChar x s | x <- xs, s <- text]

到目前为止一切顺利,我已经定义了wrapshift函数(我认为它是罪魁祸首)我的加密字符,但是当我编译它时,它显示如下错误

encrypt2.hs:27:18: error:
parse error on input `='
Perhaps you need a 'let' in a 'do' block?
e.g. 'let x = 5' instead of 'x = 5'
   |
27 |  Where wrapshift = let shift' = ord(shift) `mod` 26
   |                  ^
Failed, 0 modules loaded.

我知道由于我的源文件中的tabs而发生了这类错误,但我检查了整个文件并且没有发现任何错误。我认为我在这里做错了我不会知道吗?为什么会发生这种错误请帮助我,我被击中了。谢谢你

1 个答案:

答案 0 :(得分:5)

Where wrapshift

请注意此处的where未突出显示为关键字。那是因为你用大写字母“W”拼写它。

因此它被解释为标识符而不是关键字,使Where wrapshift被解释为前一个表达式的参数(即c Where wrapshift被视为函数调用)。这就是为什么它不期望=那里。