Numbers in Haskell

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

3 Responses to “Numbers in Haskell”

  1. Paul Says:

    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)!

  2. Nick Says:

    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 .

  3. Nick Says:

    Er, I mean here. Heh-heh.

Leave a Reply