[plug] qsort

The Thought Assassin assassin at live.wasp.net.au
Mon Jun 10 17:23:03 WST 2002


On Mon, 10 Jun 2002, Cameron Patrick wrote:
> On Mon, Jun 10, 2002 at 01:34:55PM +0800, The Thought Assassin wrote:
> > qsort lt []     = []
> > qsort lt (x:xs) = qsort lt y ++ [x] ++ qsort lt z
> >                   where (y,z) = partition (lt x) xs
> > Now that is a thing of beauty.
> Ahh... Haskell... are you a UWA student by any chance?  ;)

However did you guess? :)

> Here's a challenge: rewrite my bogosort in Haskell using monads.

Easy. It's still nicer than your C version, though of course there are
limits to how nice an implementation bogosort can ever really be:


bogosort lt xs | is_sorted lt xs = putStr (show xs)
               | otherwise       = do a <- randint
                                      b <- randint
                                      bogosort lt (map (swap a b) [0..z])
                 where swap a b i | i == a    = xs!!b
                                  | i == b    = xs!!a
                                  | otherwise = xs!!i
                       z = length xs - 1
                       randint = getStdRandom (randomR (0,z))
                       is_sorted lt xs = all lt (zip xs (tail xs))

You like?

-Greg



More information about the plug mailing list