Skip to content

Instantly share code, notes, and snippets.

@shoooe
Created February 18, 2014 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shoooe/9075835 to your computer and use it in GitHub Desktop.
Save shoooe/9075835 to your computer and use it in GitHub Desktop.
Caesar's chiper -- Haskell exercise from §5.5 of "Programming in Haskell"
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