Skip to content

Instantly share code, notes, and snippets.

@shmookey
Last active February 8, 2017 03:05
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 shmookey/0080fd1c03006709ba820f3106dc22a4 to your computer and use it in GitHub Desktop.
Save shmookey/0080fd1c03006709ba820f3106dc22a4 to your computer and use it in GitHub Desktop.
words that rot13 to other words
import Data.Char (chr, ord)
import Data.List (sortOn)
rot13 :: String -> String
rot13 = map rot where
rot x =
let
n = ord x
isUpper = n >= 65 && n <= 90
isLower = n >= 97 && n <= 122
in
if isUpper then chr (((n - 65 + 13) `mod` 26) + 65)
else if isLower then chr (((n - 97 + 13) `mod` 26) + 97)
else x
showPair :: (String, String) -> IO ()
showPair (x,y) = do
putStr x
putStr " / "
putStrLn y
main :: IO ()
main = do
dict <- (reverse . sortOn length . lines) <$> readFile "/usr/share/dict/web2"
let rdict = map rot13 dict
let results = filter (flip elem dict . snd) (zip dict rdict)
mapM_ showPair results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment