ROT 13/47 Encoder/Decoder in Haskell

rot is a simple UNIX utility for encoding and decoding text as either ROT 13 or ROT 47. It was written in Haskell and works like most UNIX utilities - it reads it's input from standard input and writes it's output to standard output. rot can also read it's input from a file. The core ROT functions consist of a small amount of Haskell:

module Rot where
import Data.Map
import qualified Data.Map as Map
import Char


asciiAlphabet   = "!\"#$%&'()*+,-./" ++ ['0'..'9'] ++ ":;<=>?@" ++ ['A'..'Z'] ++ "[\\]^_`" ++ ['a'..'z'] ++ "{|}~"

rot13Alphabet   = ['a' .. 'z']

shiftStr        :: Int -> String -> String
shiftStr n str  = let (a, b) = splitAt n str
                  in b ++ a

rot47           :: String -> String
rot47 str       = let a = asciiAlphabet
                  in rot str a (shiftStr 47 a)

unrot47         :: String -> String
unrot47 str     = let a = asciiAlphabet
                  in rot str (shiftStr 47 a) a

rot13           :: String -> String
rot13 str       = rot str (appendMap toUpper rot13Alphabet) 
                          (appendMap toUpper (shiftStr 13 rot13Alphabet))

unrot13         :: String -> String
unrot13 str     = rot str (appendMap toUpper (shiftStr 13 rot13Alphabet))
                          (appendMap toUpper rot13Alphabet) 

appendMap       :: (a -> a) -> [a] -> [a]
appendMap fn s  = s ++ (fmap fn s)

rot             :: String -> String -> String -> String
rot str a b     = [ case (Map.lookup c lookupTable) of 
                      Nothing -> c
                      Just x  -> x
                    | c <- str ]
                  where lookupTable = Map.fromList $ zip a b
		

The usage for rot is:

rot: Usage: rot [OPTIONS...]

  -h       --help         display this help and exit
           --to-13        encode using ROT13 encoding
           --from-13      decode using ROT13 encoding
           --to-47        encode using ROT47 encoding
           --from-47      decode using ROT47 encoding
  -i FILE  --input=FILE   read input from file
  -o FILE  --output=FILE  write output to file

		

Download: rot-1.0.tar.gz