Towers of Hanoi: Difference between revisions

→‎{{header|Haskell}}: laziness is better than tail recursion
(add Tcl)
(→‎{{header|Haskell}}: laziness is better than tail recursion)
Line 344:
 
hanoi :: Integer -> a -> a -> a -> [(a, a)]
hanoi =0 hanoi'_ _ _ = [] where
hanoi n a b c = hanoi' k(n-1) 0a _c _b _++ [(a,b)] =++ hanoi (n-1) c b ka
hanoi' k n a b c = hanoi' ((a,b):(hanoi' k (n-1) c b a)) (n-1) a c b
 
Here hanoi' uses an accumulator argument for the "following" moves.
 
One can use this function to produce output, just like the other programs:
Line 359 ⟶ 356:
hanoiM :: Integer -> IO ()
hanoiM n = hanoiM' n 1 2 3 where
hanoiM' 0 a_ b_ c_ = return ()
hanoiM' n a b c = do
hanoiM' (n-1) a c b
Anonymous user