or1ko's diary

日々を書きます

08

言語処理100本ノック 2015

08. 暗号文
与えられた文字列の各文字を,以下の仕様で変換する関数cipherを実装せよ.

英小文字ならば(219 - 文字コード)の文字に置換
その他の文字はそのまま出力
この関数を用い,英語のメッセージを暗号化・復号化せよ.

import Data.Char

main = putStrLn $ cipher $ cipher s
  where
    s = "Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can."

cipher s =  map f s
  where
   f c | isLower c = chr (219 - ord c)
       | otherwise = c

逆関数が必要かと思ったけど、不要だった。
こういう関数を対合というらしい。

対合 - Wikipedia