module Exercise_12 where import System.IO import Data.Function import Data.List (nub, sortBy, groupBy, partition) import Data.Bifoldable (biconcat) 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 s = [s!!pred i | i <- 1 `enumFromTo` length s, isPrime i] ++ [s!!pred i | i <- 1 `enumFromTo` length s, not $ isPrime i] {-WETT-} encrypt :: String -> String encrypt s = map (s!!) . map pred . biconcat . partition isPrime $ 1 `enumFromTo` length s decrypt :: String -> String decrypt s = map fst $ sortBy (compare `on` snd) $ s `zip` (biconcat . partition isPrime $ 1 `enumFromTo` length s) {-TTEW-}