diff options
Diffstat (limited to 'Haskell-book/11/Cipher.hs')
| -rw-r--r-- | Haskell-book/11/Cipher.hs | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/Haskell-book/11/Cipher.hs b/Haskell-book/11/Cipher.hs new file mode 100644 index 0000000..92e0e7d --- /dev/null +++ b/Haskell-book/11/Cipher.hs @@ -0,0 +1,24 @@ +module Cipher where + +import Data.Char +import Data.List + +decode :: (Char, Int) -> Char +decode (x, y) = chr ((mod ((ord x) - 65 + y) 26) + 65) + +decodeWithKey :: [Int] -> String -> String +decodeWithKey _ [] = [] +decodeWithKey ys (' ':xs) = ' ' : (decodeWithKey ys xs) +decodeWithKey (y:ys) (x:xs) = (decode (x, y)) : (decodeWithKey ys xs) + +vigenere :: String -> String +vigenere = decodeWithKey (cycle [0, 11, 11, 24]) + +caeser :: String -> String +caeser = decodeWithKey (cycle [11]) + +main :: IO () +main = do + putStrLn (vigenere "MEET AT DAWN") + putStrLn (caeser "MEET AT DAWN") + return () |
