Skip to content

Instantly share code, notes, and snippets.

@Shuumatsu
Created October 8, 2017 07:50
Show Gist options
  • Save Shuumatsu/467589445d592b7070cfa71c7694e38b to your computer and use it in GitHub Desktop.
Save Shuumatsu/467589445d592b7070cfa71c7694e38b to your computer and use it in GitHub Desktop.
module ShiftString where
import Data.Char
data CharacterNumber
= Lower Int
| Upper Int
add' :: CharacterNumber -> Int -> CharacterNumber
add' (Lower a) b = Lower (a + b)
add' (Upper a) b = Upper (a + b)
mod' :: CharacterNumber -> Int -> CharacterNumber
mod' (Lower a) b = Lower (a `mod` b)
mod' (Upper a) b = Upper (a `mod` b)
let2int :: Char -> CharacterNumber
let2int c
| isLower c = Lower (ord c - ord 'a')
| otherwise = Upper (ord c - ord 'A')
int2let :: CharacterNumber -> Char
int2let (Lower n) = chr $ ord 'a' + n
int2let (Upper n) = chr $ ord 'A' + n
shift :: Int -> Char -> Char
shift n c = int2let $ (let2int c `add'` n) `mod'` 26
encode :: Int -> String -> String
encode n xs = [shift n x | x <- xs]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment