September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,32 +1,23 @@
{-# LANGUAGE LambdaCase #-}
module Main where
import Data.Char (ord, chr, isUpper, isAlpha)
import Control.Error (tryRead, tryAt)
import Control.Monad.Trans (liftIO)
import Control.Monad.Trans.Except (ExceptT, runExceptT)
caesar, uncaesar :: Int -> String -> String
caesar = (<$>) . tr
import Data.Char
import System.Exit (die)
import System.Environment (getArgs)
uncaesar = caesar . negate
tr :: Int -> Char -> Char
tr offset c
| isAlpha c = chr $ intAlpha + mod ((ord c - intAlpha) + offset) 26
| otherwise = c
where
intAlpha =
ord
(if isUpper c
then 'A'
else 'a')
main :: IO ()
main = runExceptT parseKey >>= \case
Left err -> die err
Right k -> interact $ caesar k
parseKey :: (Read a, Integral a) => ExceptT String IO a
parseKey = liftIO getArgs >>=
flip (tryAt "Not enough arguments") 0 >>=
tryRead "Key is not a valid integer"
caesar :: (Integral a) => a -> String -> String
caesar k = map f
where f c = case generalCategory c of
LowercaseLetter -> addChar 'a' k c
UppercaseLetter -> addChar 'A' k c
_ -> c
addChar :: (Integral a) => Char -> a -> Char -> Char
addChar b o c = chr $ fromIntegral (b' + (c' - b' + o) `mod` 26)
where b' = fromIntegral $ ord b
c' = fromIntegral $ ord c
main = mapM_ print [encoded, decoded]
where
encoded = caesar (-114) "Curio, Cesare venne, e vide e vinse ? "
decoded = uncaesar (-114) encoded