Created
June 8, 2016 22:31
-
-
Save SamirTalwar/2f93b85c08918d91015d47d45529c82e to your computer and use it in GitHub Desktop.
String rotation (rot13, etc.) 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
| -- Usage: | |
| -- rot 13 "Uryyb, jbeyq!" | |
| -- --> "Hello, world!" | |
| import Control.Applicative ((<|>)) | |
| import Data.Maybe (fromMaybe) | |
| import System.Environment (getArgs) | |
| caesarCipher :: Int -> [a] -> [(a, a)] | |
| caesarCipher n xs = zip xs (drop n (cycle xs)) | |
| rot :: Int -> String -> String | |
| rot n = | |
| let uppers = caesarCipher n ['A'..'Z'] | |
| lowers = caesarCipher n ['a'..'z'] | |
| in map (\c -> fromMaybe c (lookup c uppers <|> lookup c lowers)) | |
| main = do | |
| n <- (read . head) <$> getArgs | |
| contents <- getContents | |
| putStr $ rot n contents |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
mentioned and used your code with attribution in the @1HaskellADay exercise and solution for Y2017.M08.D04: https://github.com/geophf/1HaskellADay/blob/master/exercises/HAD/Y2017/M08/D04/Solution.hs