import Data.List (unfoldr) import System.Random headTail (x:xs) = (x, xs) bubbleSort :: Ord a => [a] -> [a] bubbleSort = unfoldr (fmap headTail . sort) where sort [] = Nothing sort (x:xs) = Just $ foldl f [x] xs f (z:zs) y | z < y = z : y : zs | otherwise = y : z : zs main :: IO () main = do xs <- randomRs ('a', 'z') `fmap` getStdGen print $ bubbleSort $ take 42 xs