lær*_*n91 1 haskell functional-programming
我是haskell.am的新手,试图为给定的纯文本加密实现加密.下面是我的示例代码
encryptChar :: Int -> Char -> Char
encryptChar shift c
| canEncrypt c = chr(ord c + wrapshift)
| isUpper c = c
where wrapshift = let shift' = 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 _ '?' = ' '
encryptChar _ c = c
Run Code Online (Sandbox Code Playgroud)
当我的字符包含一个数字时,它必须使用特殊符号进行加密,例如在上面的代码中,当我的字符包含1它时必须替换\它,我发现它是haskell中的保留字符.但我怎样才能使用这些字符加密?
简答:使用'\\'.
大多数编程语言使用转义序列 [wiki]来表示特殊字符,并使用反斜杠作为转义序列的开头.
然后将反斜杠字符表示为转义反斜杠,因此'\\'.
就像维基百科文章中的表格所说:
Run Code Online (Sandbox Code Playgroud)Escape Sequence Unicode Literal Characters placed into string \\ U+005C backslash (\)