Skip to content

Instantly share code, notes, and snippets.

@SamirTalwar
Created June 8, 2016 22:31
Show Gist options
  • Select an option

  • Save SamirTalwar/2f93b85c08918d91015d47d45529c82e to your computer and use it in GitHub Desktop.

Select an option

Save SamirTalwar/2f93b85c08918d91015d47d45529c82e to your computer and use it in GitHub Desktop.
String rotation (rot13, etc.) in Haskell.
-- 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
@geophf
Copy link

geophf commented Aug 7, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment