module Exercise_12 where import Data.List --(nub) import Control.Arrow {-H12-} 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] -- Shorter Version BELOW {-WETT-} encrypt :: String -> String decrypt :: String -> String f = not . isPrime encrypt = map snd . sortOn (f . fst) . zip (enumFrom 1) decrypt = map snd . sort <<< zip =<< sortOn f . enumFromTo 1 . length {-TTEW-} {-MCCOMMENT Not sure if we are allowed to change the signature (this would be shorter -> 34t): encrypt :: Ord a => [a] -> [a] decrypt :: Ord a => [a] -> [a] encrypt = map snd . sortOn (not . isPrime . fst) . zip (enumFrom 1) decrypt = map snd . sort <<< zip =<< encrypt . enumFromTo 1 . length -} encryptShort :: Ord a => [a] -> [a] decryptShort :: Ord a => [a] -> [a] encryptShort = map snd . sortOn (not . isPrime . fst) . zip (enumFrom 1) decryptShort = map snd . sort <<< zip =<< encryptShort . enumFromTo 1 . length