module Exercise_12 where import Data.List 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-} isPrimeTuple = isPrime . fst --only checks whether first Element of Tuple is Prime encrypt :: String -> String encrypt xs = snd $ unzip $ fst a ++ snd a where a = partition isPrimeTuple $ enumFrom 1 `zip` xs decrypt :: String -> String decrypt xs = snd $ unzip $ sort $ zip primesxs xs ++ filter (`notElem` primesxs) (enumFrom 1) `zip` drop (length primesxs) xs where primesxs = primes $ length xs {-TTEW-} --recursive Implementation decrypt2 :: String -> String decrypt2 xs = decrypt' [1..length xs] xs (drop ( length $ primes $ length xs) xs) decrypt' (y:ys) (x:xs) (a:as) | isPrime y = x : decrypt' ys xs (a:as) | otherwise = a : decrypt' ys (x:xs) as decrypt' ys xs _ = take (length ys) xs