module Exercise_12 where import Data.List import Data.Maybe import Data.Function 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 str = let ps = primes $ length str (l, r) = partition (isPrime . fst) $ zip [1..] str in map snd $ l ++ r decrypt :: String -> String decrypt str = let ps = primes $ length str l = zip ps str index = [a|a <- [1..(length str)], a `notElem` ps] r = zip index (drop (length l) str) str' = sortOn fst (l ++ r) in map snd str' {-TTEW-}