-
-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathCaesarCipher.hs
More file actions
22 lines (18 loc) · 845 Bytes
/
CaesarCipher.hs
File metadata and controls
22 lines (18 loc) · 845 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
module Ciphers.CaesarCipher(encrypt,decrypt) where
import Data.List (elemIndex)
import Data.Maybe (fromMaybe)
-- | Encrypting a string maps each character to the character `n` elements
-- after it in the given alphabet, where `n`is the provided key
encrypt :: String -> Int -> String -> String
encrypt = helper (+)
-- | Decrypting a string maps each character to the character `n` elements
-- before it in the given alphabet, where `n` is the provided key
decrypt :: String -> Int -> String -> String
decrypt = helper (-)
helper :: (Int -> Int -> Int) -> String -> Int -> String -> String
helper _ input 0 _ = input
helper _ input _ [] = input
helper op input key alphabet = map (\x -> fromMaybe x (mappedChar $ elemIndex x alphabet)) input
where
len = length alphabet
mappedChar = fmap (\c -> alphabet !! (c `op` key `mod` len))