Let us discuss the various implementations of theFIBONACCIBUTTSORT.
Name:
Anonymous2009-09-07 12:38
-- buttsort.hs - an implementation of the Fibonacci Buttsort
import Data.Char (isSpace)
import Control.Monad.State
data Tag = Tag String
deriving (Show, Eq)
class Text a where
value :: a -> String
instance Text Char where
value = (:[])
instance (Text a) => Text [a] where
value = concatMap value
(<<) :: (Text a) => Tag -> a -> String
infixr <<
(Tag t) << x = "[" ++ t ++ "]" ++ value x ++ "[/" ++ t ++ "]"
[b, i, o, u, m] = map Tag ["b", "i", "o", "u", "m"]
buttsortW, buttsortC :: String -> String
buttsortW = (b <<) . (i <<) . unwords . zipWith (<<) (cycle [u, o]) . words
buttsortC = (b <<) . (i <<) . concat . flip evalState (cycle [u, o]) . mapM f
where f c = if isSpace c then return [c]
else do t <- get
modify tail
return $ head t << c