Return to Snippet

Revision: 48644
at July 7, 2011 01:05 by rtperson


Initial Code
--Haskell Towers of Hanoi
-- ... because you know you want to...

import System.Environment

-- you are moving from a to b using c as an intermediate
hanoi :: Integer -> a -> a -> a -> [(a,a)]
hanoi 0 _ _ _ = []
hanoi n a b c = hanoi (n - 1) a c b ++ [(a,b)] ++ hanoi (n - 1) c b a

hanoiIO :: Integer -> IO ()
hanoiIO n = mapM_ f $ hanoi n "one" "two" "three" where
    f (x,y) = putStrLn $ "Move " ++ show x ++ " to " ++ show y
    
-- for some reason this doesn't work in GHCi. Use main' in GHCi
main :: IO ()
main = do
    args <- getArgs
    hanoiIO $ read (args !! 0)
    
main' = hanoiIO 3

Initial URL

                                

Initial Description

                                

Initial Title
Towers of Hanoi in Haskell

Initial Tags

                                

Initial Language
Haskell