import List
import Random
shuffleWith :: IO [Int]
shuffleWith = randomIO >>= seed -> return $ randoms (mkStdGen seed)
shuffle :: (Ord a) => [a] -> IO [a]
shuffle xs = shuffleWith >>= nums -> return $ (map snd . sort . zip nums) xs
getInt :: IO Int
getInt = do line <- getLine
return (read line :: Int)
play :: [Int] -> Int -> IO ()
play numbers steps = do
putStrLn $ (unwords . map show) numbers
putStr "Reverse how many? "
flipCount <- getInt
let numbers' = (reverse . take flipCount) numbers ++ (drop flipCount numbers)
if numbers' == sort numbers
then putStrLn $ "Done! That took you " ++ (show steps) ++ " steps."
else play numbers' (steps + 1)
main :: IO ()
main = do
numbers < - shuffle [1..9]
play numbers 1
This entry was posted
on Monday, January 16th, 2006 at 12:38 pm and is filed under General, Programming.
You can follow any responses to this entry through the RSS 2.0 feed.
You can skip to the end and leave a response. Pinging is currently not allowed.
January 16th, 2006 at 6:44 pm
Hi there, I’m the author of the “Let’s play a game” post that you wrote this in response to. Sorry about the Markdown problems you had on my site — you may have been getting bit by a Markdown/Smartypants interaction that I’ve now fixed. Anyway, I cleaned up your post here. Thanks for reading (and writing)!
December 3rd, 2006 at 8:17 pm
I realize this is a few *months* late, but I had written a version myself that I had not submitted. I’m still rather a noob at Haskell, and your code encouraged me to hack at it until I could understand it. I wrote about it at .
December 3rd, 2006 at 8:19 pm
Er, I mean here. Heh-heh.