2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
32
Task/Caesar-cipher/Haskell/caesar-cipher-2.hs
Normal file
32
Task/Caesar-cipher/Haskell/caesar-cipher-2.hs
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue