summaryrefslogtreecommitdiff
path: root/Haskell-book/09/src/Cipher.purs
blob: d5605c2f4b340193c22c119b9bfecb5d3c720607 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
module Cipher where

import Data.Char (fromCharCode, toCharCode)
import Data.String (fromCharArray, toCharArray)
import Prelude

caesar :: Int -> String -> String
caesar key s = fromCharArray $ map shiftRight (toCharArray s)
  where shiftRight char = fromCharCode $ mod (ord char) 26
        ord = (add key) <<< toCharCode

unCaesar :: Int -> String -> String
unCaesar key s = fromCharArray $ map shiftLeft (toCharArray s)
  where shiftLeft char = fromCharCode $ ord char
        ord chr = ((toCharCode chr) - (mod key 26)) + 26