Skip to content

Instantly share code, notes, and snippets.

@ford-prefect
Created February 23, 2016 14:23
Show Gist options
  • Save ford-prefect/7bb6b7961c928e0264d7 to your computer and use it in GitHub Desktop.
Save ford-prefect/7bb6b7961c928e0264d7 to your computer and use it in GitHub Desktop.
import Data.Char
rot13 :: String -> String
rot13 s = case length s of
0 -> ""
1 -> [rot13c (head s)]
_ -> rot13c (head s) : rot13 (tail s)
where
rot13c c
| isAsciiLower c = rot13b 'a' c
| isAsciiUpper c = rot13b 'A' c
| otherwise = error "Non-ASCII character"
rot13b b c = chr (ord b + (ord c + 13 - ord b) `mod` 26)
main :: IO()
main = print (rot13 "abcABCnopNOP")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment