Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,16 @@
module Caesar (caesar, uncaesar) where
import Data.Char
caesar, uncaesar :: (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
uncaesar k = caesar (-k)
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

View file

@ -0,0 +1,24 @@
import Data.Bool (bool)
import Data.Char (chr, isAlpha, isUpper, ord)
---------------------- CAESAR CIPHER ---------------------
caesar, uncaesar :: Int -> String -> String
caesar = fmap . tr
uncaesar = caesar . negate
tr :: Int -> Char -> Char
tr offset c
| isAlpha c =
chr
. ((+) <*> (flip mod 26 . (-) (offset + ord c)))
$ bool 97 65 (isUpper c)
| otherwise = c
--------------------------- TEST -------------------------
main :: IO ()
main = do
let k = -114
cipher = caesar k
plain = "Curio, Cesare venne, e vide e vinse ? "
mapM_ putStrLn $ [cipher, uncaesar k . cipher] <*> [plain]

View file

@ -0,0 +1,32 @@
{-# LANGUAGE LambdaCase #-}
module Main where
import Control.Error (tryRead, tryAt)
import Control.Monad.Trans (liftIO)
import Control.Monad.Trans.Except (ExceptT, runExceptT)
import Data.Char
import System.Exit (die)
import System.Environment (getArgs)
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