Last active
August 29, 2015 14:08
-
-
Save chomado/59ff4d3914171ffa9bd0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Data.Char | |
| -- 文字c を シフト数n だけずらす関数 | |
| shift :: Int -> Char -> Char | |
| shift n c | |
| | isLower c = chr $ ord 'a' + ((ord c - ord 'a') + n) `mod` 26 | |
| | isUpper c = chr $ ord 'A' + ((ord c - ord 'A') + n) `mod` 26 | |
| | otherwise = c | |
| --シーザー暗号化する関数 | |
| encode :: Int -> String -> String | |
| encode n str = [shift n c | c <- str] | |
| -- ROT13変換 | |
| rot13 :: String -> String | |
| rot13 str = encode 13 str | |
| main = do | |
| print $ encode 10 "Hello, World!" -- "Rovvy, Gybvn!" | |
| let hello13 = rot13 "Hello, World!" | |
| print hello13 -- "Uryyb, Jbeyq!" | |
| print $ rot13 hello13 -- "Hello, World!" (もとに戻る) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment