Skip to content

Instantly share code, notes, and snippets.

@shigemk2 shigemk2/caesar.hs
Created Feb 18, 2015

Embed
What would you like to do?
import Data.Char
-- ordで数値に変換して、offsetをプラスして、それをchrで元に戻している。
-- ordやchrがあれば、とりあえずBrainfuckのインタプリタが作れそう。ただ、ファイルを読み込むので副作用を考慮しないといけないけども。
encode :: Int -> String -> String
encode offset msg = map (\c -> chr $ ord c + offset) msg
-- 若干手抜き
decode :: Int -> String -> String
decode shift msg = encode (negate shift) msg
main = do
print $ encode 3 "hey mark"
print $ encode 5 "please instruct your men"
print $ encode 1 "to party hard"
print $ decode 3 "kh|#pdun"
print $ decode 5 "uqjfxj%nsxywzhy%~tzw%rjs"
print $ decode 1 "up!qbsuz!ibse"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.