Better main

This commit is contained in:
EEva 2019-07-19 18:56:37 +03:00
parent b378d13ebb
commit 7afe4dd84c
2 changed files with 10 additions and 3 deletions

View File

@ -4,13 +4,14 @@ import Data.Char
import Data.Maybe import Data.Maybe
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
wtf = '¿'
downGrade :: String -> String downGrade :: String -> String
downGrade = fmap (clip . toUpper) downGrade = fmap (clip . toUpper)
where where
clip c | elem (toUpper c) alphabet = toUpper c clip c | elem (toUpper c) alphabet = toUpper c
clip ' ' = ' ' clip ' ' = ' '
clip _ = '_' clip _ = wtf
encode :: Int -> String -> String encode :: Int -> String -> String
encode key msg = unwords $ encodeWord key <$> (words $ downGrade msg) encode key msg = unwords $ encodeWord key <$> (words $ downGrade msg)
@ -25,7 +26,7 @@ encodeWord :: Int -> String -> String
encodeWord k = fmap (substitute $ mapping k) encodeWord k = fmap (substitute $ mapping k)
substitute :: [(Char,Char)] -> Char -> Char substitute :: [(Char,Char)] -> Char -> Char
substitute m e = fromMaybe '_' $ lookup e m substitute m e = fromMaybe wtf $ lookup e m
mapping :: Int -> [(Char,Char)] mapping :: Int -> [(Char,Char)]
mapping k = zip alphabet (alphabet `rotateBy` k) mapping k = zip alphabet (alphabet `rotateBy` k)

View File

@ -4,4 +4,10 @@ import Caesar
-- Incredible! -- Incredible!
main :: IO () main :: IO ()
main = putStrLn $ decode 1 $ encode 1 "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam rhoncus elit ac nunc ornare fringilla. Sed pharetra eros pulvinar pellentesque aliquam. Donec in pretium elit. Aliquam vulputate tincidunt venenatis. Morbi quis metus nisi. Vestibulum nunc est, fringilla vel posuere sit amet, ultricies at nulla. Nunc bibendum odio vel augue mollis congue. Mauris vitae gravida quam. Vivamus dictum vehicula nisl non egestas. Cras ultrices ullamcorper diam non lacinia. Sed mollis quam augue, ac dictum erat auctor sit amet." main = do
let t = "I like strawberries!"
putStrLn t
putStrLn $ encode 3 t
putStrLn $ id' 3 t
id' n = (decode n) . (encode n)