rot13を行うプログラムを書いてみました

パッと見、何が書かれているかわからなくするプログラムです。安全な暗号じゃないんで、それなりの用途に使ってください。
ファイル: rot13.hs

#!/usr/bin/env runghc

module Main (main) where

import Data.Char (chr, ord)
import System (getArgs)

rot13 :: String -> String
rot13 = map (chr . conv . ord)
  where
    conv :: Int -> Int
    conv i
      | (i >= ord 'a') && (i <= ord 'm') = i + 13
      | (i >= ord 'n') && (i <= ord 'z') = i - 13
      | (i >= ord 'A') && (i <= ord 'M') = i + 13
      | (i >= ord 'N') && (i <= ord 'Z') = i - 13
      | otherwise = i

readFiles :: [String] -> IO String
readFiles [] = getContents
readFiles files = return . concat =<< mapM readFile files

main :: IO ()
main = putStr . rot13 =<< readFiles =<< getArgs

以下、実行例です。rot13の変換を2回行うと元に戻ります。

$ echo hoge | ./rot13.hs
ubtr
$ echo hoge | ./rot13.hs | ./rot13.hs
hoge

参照: ROT13 - Wikipedia