-
Notifications
You must be signed in to change notification settings - Fork 0
/
cipher.hs
34 lines (27 loc) · 857 Bytes
/
cipher.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
module Cipher (caesar, unCaesar) where
import Data.Char
import System.IO
caesar :: IO ()
caesar = do
hSetBuffering stdout NoBuffering
putStr "Please enter the string to be ciphered: "
str <- getLine
putStr "Please enter the number of shifts: "
i <- readLn
print $ caesar' str i
unCaesar :: IO ()
unCaesar = do
hSetBuffering stdout NoBuffering
putStr "Please enter the string to be unciphered: "
str <- getLine
putStr "Please enter the number of unshifts: "
i <- readLn
print $ unCaesar' str i
caesar' :: [Char] -> Int -> [Char]
caesar' s i = map shiftChar s where
shiftChar c = chr $ rem (ord c + i') 256 where
i' = rem (abs i) 256
unCaesar' :: [Char] -> Int -> [Char]
unCaesar' s i = map shiftChar s where
shiftChar c = chr $ if num < 0 then 256 + num else num where
num = ord c - rem (abs i) 256