Haskell Quiz/Text Munger/Solution Dolio

From HaskellWiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.


This solution makes use of the random monad elsewhere on the wiki.

module Main where
import Control.Monad
import Data.Char
import Data.List
import System.Random
import MonadRandom

permute [] = return []
permute l  = do r <- getRandomR (0, n)
                let (a, e:b) = splitAt r l
                l' <- permute (a ++ b)
                return $ e : l'
 where n = length l - 1

munge l
    | isAlpha h && length l > 2 = ((h:) . (++[f])) `liftM` permute m
    | otherwise                 = return l
 where h = head l
       f = last l
       m = tail (init l)

main = do g <- getStdGen
          interact $ flip evalRand g . liftM join . mapM munge . groupBy f
 where f a b = isAlpha a == isAlpha b