module Exercise_12 where import Control.Applicative --why is this needed on the submission server, <*> should be part of Prelude (╯°□°)╯︵ ┻━┻) import Control.Arrow import Control.Monad import Data.List isPrime :: Int -> Bool isPrime n | n <= 1 = False | otherwise = primeAux (n - 1) where primeAux 1 = True primeAux i = n `mod` i /= 0 && primeAux (i - 1) -- returns all primes up to the passed number primes :: Int -> [Int] primes n = [i | i <- [2 .. n], isPrime i] -- encrypt :: String -> String -- encrypt s = -- (map fst . ((++) <$> fst <*> snd)) -- (partition (isPrime . snd) (zip s [1 .. length s])) -- decrypt :: String -> String -- decrypt y = map fst $ sortOn snd (c ++ b) -- where -- a = splitAt ((length . primes) (length y)) y -- b = (zip (fst a) (primes $ length y)) -- c = zip (snd a) ([1 .. length y] \\ primes (length y)) -- 1: 35 -- ges: 110 -- 67 token {-WETT-} encrypt :: String -> String encrypt = zip (enumFrom 1) >>> partition (isPrime . fst) >>> uncurry (++) >>> map snd decrypt :: String -> String decrypt y = (splitAt ((length . primes) l) >>> zip (primes l) *** zip (enumFrom 1 \\ primes l) >>> uncurry (++) >>> sort >>> map snd) y where l = length y {-TTEW-}