Created
January 31, 2013 02:29
-
-
Save akanehara/4679430 to your computer and use it in GitHub Desktop.
すごいHaskell読書会 in 大阪 #4 第6章「モジュール」持ち寄り練習問題 解答例
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
module Toy.Rot13 (encrypt) where | |
import Data.Char | |
lowerA = ord 'a' | |
lowerZ = ord 'z' | |
upperA = ord 'A' | |
upperZ = ord 'Z' | |
encrypt :: String -> String | |
encrypt = rot 13 | |
rot :: Int -> String -> String | |
rot r = map (rotChar r) | |
rotChar :: Int -> Char -> Char | |
rotChar r c = chr $ go r $ ord c | |
where | |
go r x | |
| lowerA <= x && x <= lowerZ = rotate r lowerA lowerZ x | |
| upperA <= x && x <= upperZ = rotate r upperA upperZ x | |
| otherwise = x | |
rotate r a z x = | |
let width = (z - a + 1) | |
in (x - a + r) `rem` width + a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment