unsort bis
Posted on 20 May 2007
Boy, do I feel like a big dummy!
Here is a super short and functional version I saw from a Pythonista on reddit.
import Data.List ( sort )
import System.Random ( Random, RandomGen, randoms, getStdGen )
main :: IO ()
main =
do gen <- getStdGen
interact $ unlines . unsort gen . lines
unsort :: (Ord x, RandomGen g) => g -> [x] -> [x]
unsort g es = [ y | (_,y) <- sort $ zip rs es ]
where rs = randoms g :: [Integer]
edit!
Heffalump points out that (i) if you have duplicates in
rs, you don't get scrambling for the relevant bits (ii) this requires Ord x, which it really ought not to (although that is ani improvement from the array thing where I had to lock the type down for some reason)
edit deux
Hmm... what does it mean to get a list of random arbitrary precision integers?