Skip to content

Instantly share code, notes, and snippets.

@akanehara
Created January 31, 2013 02:29
Show Gist options
  • Save akanehara/4679430 to your computer and use it in GitHub Desktop.
Save akanehara/4679430 to your computer and use it in GitHub Desktop.
すごいHaskell読書会 in 大阪 #4 第6章「モジュール」持ち寄り練習問題 解答例
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