Created
March 25, 2018 13:30
-
-
Save neevek/7e74ef2c5e78ca5e4bf331865ac26d2f to your computer and use it in GitHub Desktop.
CaesarCipher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.Char | |
data CaesarCipherShift = CipherShift Int | |
caesarEncode :: CaesarCipherShift -> String -> String | |
caesarEncode _ [] = [] | |
caesarEncode (CipherShift shiftCount) (x:xs) | |
| shiftedOrd > printableMax = | |
(chr $ ((shiftedOrd - 1 - printableMin) `mod` printableRange) + printableMin) : | |
(caesarEncode (CipherShift shiftCount) xs) | |
| shiftedOrd < 0 = | |
(chr $ printableMax + ((shiftedOrd + 1) `rem` printableMax)) : | |
(caesarEncode (CipherShift shiftCount) xs) | |
| shiftedOrd < printableMin = | |
(chr $ ((shiftedOrd + 1) `mod` printableRange) + printableRange) : | |
(caesarEncode (CipherShift shiftCount) xs) | |
| otherwise = chr shiftedOrd : (caesarEncode (CipherShift shiftCount) xs) | |
where | |
printableMin = 0x20 | |
printableMax = 0x7e | |
printableRange = printableMax - printableMin | |
shiftedOrd = ord x + shiftCount | |
caesarDecode :: CaesarCipherShift -> String -> String | |
caesarDecode (CipherShift shiftCount) xs = | |
caesarEncode (CipherShift (negate shiftCount)) xs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: