Skip to content

Instantly share code, notes, and snippets.

@chomado
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save chomado/59ff4d3914171ffa9bd0 to your computer and use it in GitHub Desktop.

Select an option

Save chomado/59ff4d3914171ffa9bd0 to your computer and use it in GitHub Desktop.
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