tag:blogger.com,1999:blog-21123659.post1759799152904740938..comments2014-08-24T08:38:43.131+02:00Comments on koweycode: unsortkoweyhttp://www.blogger.com/profile/11175806459477851520noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-21123659.post-59993704619789203872007-05-20T20:46:00.000+02:002007-05-20T20:46:00.000+02:00Here's my implementation. I think it's somewhat mo...Here's my implementation. I think it's somewhat more idiomatic. I have never used mutable arrays in Haskell. I borrow your clever unfoldr but my code is a lot shorter because I'm just using lists. I'm no Haskell expert either, so I'm sure there is still a yet-shorter, yet-more-idiomatic version.<BR/><BR/>-- unsort -- unsorts a list<BR/>-- by Daniel Lyons <BR/><BR/>module Main where<BR/>import Data.List<BR/>import System.Random<BR/><BR/>-- ! popOut takes an index and returns the value at that index and the list<BR/>-- ! without the value at that index<BR/>popOut :: Int -> [a] -> (a, [a])<BR/>popOut x l = (l !! x, (take x l) ++ (drop (x+1) l))<BR/><BR/>-- ! unsort, given a random state and a list, scrambles the list according to<BR/>-- ! the perfect shuffle algorithm<BR/>unsort :: (RandomGen t) => t -> [a] -> [a]<BR/>unsort g l = unfoldr next (g,l)<BR/> where<BR/> next (_, []) = Nothing<BR/> next (g, l) = let (r, g2) = randomR (0, (length l) - 1) g<BR/> (i, rest) = popOut r l<BR/> in Just (i, (g2, rest))<BR/><BR/>main = do<BR/> gen <- getStdGen<BR/> interact (unlines . (unsort gen) . lines)fusiongyrohttp://www.blogger.com/profile/13365348872817885287noreply@blogger.comtag:blogger.com,1999:blog-21123659.post-5240868017950913502007-05-20T20:44:00.000+02:002007-05-20T20:44:00.000+02:00Heh... and look what turns out on programming.redd...Heh... and look what turns out on programming.reddit.com: <A HREF="http://okmij.org/ftp/Haskell/perfect-shuffle.txt" REL="nofollow">perfect shuffle algorithms</A>koweyhttp://www.blogger.com/profile/11175806459477851520noreply@blogger.comtag:blogger.com,1999:blog-21123659.post-50842596532074888382007-05-20T20:27:00.000+02:002007-05-20T20:27:00.000+02:00I don't know if I would call it at that (I cede to...I don't know if I would call it at that (I cede to anybody with algo experience); it is just one traversal through the list O(n). Hmm... not sure how other variants of unsorting would look like...koweyhttp://www.blogger.com/profile/11175806459477851520noreply@blogger.comtag:blogger.com,1999:blog-21123659.post-35743608630935437952007-05-20T19:27:00.000+02:002007-05-20T19:27:00.000+02:00So, roughly, you're doing a bubble unsort.Would be...So, roughly, you're doing a bubble unsort.<BR/>Would be nice to check wether this can also apply to other sorts, such as quicksort or merge sort.<BR/><BR/>P!Anonymousnoreply@blogger.com