Created
February 18, 2014 17:42
-
-
Save shoooe/9075835 to your computer and use it in GitHub Desktop.
Caesar's chiper -- Haskell exercise from §5.5 of "Programming in Haskell"
This file contains hidden or 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 | |
char2Int :: Char -> Int | |
char2Int l = ord l - ord 'a' | |
int2Char :: Int -> Char | |
int2Char i = chr (ord 'a' + i) | |
charAdvance :: Int -> Char -> Char | |
charAdvance i c | isLower c = int2Char (((char2Int c) + i) `mod` 26) | |
| otherwise = c | |
caesarEncode :: String -> String | |
caesarEncode s = map (charAdvance 3) s | |
caesarDecode :: String -> String | |
caesarDecode s = map (charAdvance (-3)) s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment