Skip to content

Instantly share code, notes, and snippets.

@moznion
Created July 29, 2012 12:46
Show Gist options
  • Save moznion/3198492 to your computer and use it in GitHub Desktop.
Save moznion/3198492 to your computer and use it in GitHub Desktop.
スタートHaskell 2 第2回目演習 シーザー暗号
import Data.Char
charToInt :: Char -> Int
charToInt c
| isLower c = ord c - ord 'a'
| isUpper c = ord c - ord 'A' + 26
| otherwise = ord c
intToChar :: Int -> Char
intToChar x
| x `elem` rangeOfSmall = chr $ ord 'a' + x
| x `elem` rangeOfCapital = chr $ ord 'A' + (x - 26)
| otherwise = chr x
where
rangeOfSmall = [0..25]
rangeOfCapital = [26..51]
shift :: Int -> Char -> Char
shift n c
| isLower c = intToChar $ (charToInt c + n) `mod` 26
| isUpper c = intToChar $ (charToInt c + n) `mod` 26 + 26
| otherwise = c
caesar :: Int -> String -> String
caesar n str = [shift n x | x <- str]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment