Substitution cipher: Difference between revisions

m
→‎{{header|Haskell}}: simplified convert, and generified types
m (→‎{{header|Haskell}}: simplified convert, and generified types)
Line 868:
=={{header|Haskell}}==
<lang haskell>import Data.Char (chr)
import Data.Maybe (fromMaybe)
import Data.Tuple (swap)
import System.Environment (getArgs)
Line 879 ⟶ 880:
cipherMap = zip alphabet (shuffle 20 alphabet)
 
shuffle :: Int -> String[a] -> String[a]
shuffle 0 xs = xs
shuffle n xs = iterate go xs !! n
where
Line 886:
go xs = go (drop 2 xs) <> take 2 xs
 
convert :: Eq a => [(Chara, Chara)] -> String[a] -> String[a]
convert _ []m = []map (\x -> fromMaybe x (lookup x m))
convert m (x:xs) = case lookup x m of
Just c -> c : convert m xs
Nothing -> x : convert m xs
 
runCommand :: Command -> String
Anonymous user