29 lines
686 B
Plaintext
29 lines
686 B
Plaintext
open number char monad io string
|
|
|
|
chars = "ABCDEFGHIJKLMOPQRSTUVWXYZ"
|
|
|
|
caesar _ _ [] = ""
|
|
caesar op key (x::xs) = check shifted ++ caesar op key xs
|
|
where orig = indexOf (string.upper $ toString x) chars
|
|
shifted = orig `op` key
|
|
check val | orig == -1 = x
|
|
| val > 24 = trans $ val - 25
|
|
| val < 0 = trans $ 25 + val
|
|
| else = trans shifted
|
|
trans idx = chars:idx
|
|
|
|
cypher = caesar (+)
|
|
decypher = caesar (-)
|
|
|
|
key = 2
|
|
|
|
do
|
|
putStrLn "A string to encode:"
|
|
str <- readStr
|
|
putStr "Encoded string: "
|
|
cstr <- return <| cypher key str
|
|
put cstr
|
|
putStrLn ""
|
|
putStr "Decoded string: "
|
|
put $ decypher key cstr
|