Created
March 15, 2009 14:44
-
-
Save nicolasff/79432 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import Control.Monad.Writer | |
import System(getArgs, getProgName) | |
data Column = LeftCol | MiddleCol | RightCol | |
-- describe the move of one block from column src to column dst | |
describeMove :: Int -> Column -> Column -> Writer String () | |
describeMove object src dst = do | |
tell $ "move " ++ (show object) ++ " from " ++ (colName src) ++ " to " ++ (colName dst) ++ "\n" where | |
colName LeftCol = "left" | |
colName MiddleCol = "middle" | |
colName RightCol = "right" | |
-- move a list of blocks from column x to column y with column z as temporary storage | |
move :: [Int] -> Column -> Column -> Column -> Writer String () | |
move [] _ _ _ = return () -- nothing to move. | |
move (largest:rest) src dst tmp = do | |
move rest src tmp dst -- move the top blocks from src to tmp using dest as temp | |
describeMove largest src dst -- move the largest one from src to dst | |
move rest tmp dst src -- move the tmp ones back onto the largest one on dst using src as temp | |
main :: IO () | |
main = do | |
args <- getArgs | |
if null args then do | |
getProgName >>= \argv0 -> putStrLn $ "Usage: " ++ argv0 ++ " <rings>" | |
else let n = read (head args) :: Int in do | |
putStrLn . snd . runWriter $ move [n,n-1..1] LeftCol RightCol MiddleCol |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment