module Exercise_12 where import Data.List import Data.Ord import Data.Foldable import Control.Arrow import Data.Function ((&)) import Control.Applicative ((<$>)) 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] -- Functions have, as requested, been carefully crafted for -- your viewing (dis)pleasure :,) -- There is a version below that disambiguates things, but where's the fun int that? :D {-WETT-} encrypt :: String -> String encrypt = concatMap toList.sortOn (isPrime <$> fst >>> Down) <$> zip ft1 decrypt :: String -> String decrypt s = map fst `fmap` sortOn snd <<< zip s $ flip union ft1 $ length s & primes ft1 :: [Int] ft1 = enumFrom 1 {-TTEW-} {- MCCOMMENT - A bit easier on the eyes (but using the same ammount of tokens!) : encrypt' :: String -> String encrypt' = map snd.sortOn (not.isPrime.fst).zip (enumFrom 1) decrypt' :: String -> String decrypt' s = map fst $ sortOn snd $ zip s $ (primes $ length s) `union` enumFrom 1 -}