module Exercise_12 where import Data.List (nub, partition, elemIndex, splitAt) import Data.Char (ord, chr) 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] {-WETT-} encrypt :: String -> String encrypt = map snd . uncurry (++) . partition (isPrime . fst) . zip [1..] decrypt :: String -> String decrypt cipher = [cipher !! if isPrime i then lp i -1 else i - lp i + lp n - 1 | i <- [1..n]] where n = length cipher lp = length . primes {-TTEW-}