module Exercise_12 where import Data.List 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 = map snd . flip h (isPrime . fst) . zip [1 ..] h xs f = ((++) `on` flip filter xs) f (not . f) decrypt :: String -> String decrypt s = map fst $ sortOn snd $ zip s $ flip h isPrime $ enumFromTo 1 $ length s {-MCCOMMENT Hello MC! You also asked for a cipher, so.... primesString = map pred . primes . length f s = uncurry (\a b -> reverse a ++ b) $ splitAt (length $ primesString s) s g op op2 s = f $ foldl op s $ op2 $ primesString s encrypt = g prep id decrypt = f . g reconstruct reverse . f prep s i = s !! i : take i s ++ drop (succ i) s reconstruct (s : ss) i = take i ss ++ s : drop i ss -} {-TTEW-}