Showing posts with label Hanoi. Show all posts
Showing posts with label Hanoi. Show all posts

Monday, May 31, 2010

Correcting Towers of Hanoi Haskell Solution

I've been really having fun playing with Haskell. I found a solution via Wikipedia for the Towers of Hanoi. The solution is here http://www.kernelthread.com/projects/hanoi//html/hs.html.



This solution will not compile with ghci v6.12.1. This is the corrected solution I tested and confirmed works with ghci v6.12.1:



dohanoi :: (Num a) => a -> a -> a -> a -> [(a,a)]

dohanoi 0 _ _ _ = []

dohanoi n from to using = let m=n-1

in dohanoi m from using to ++ [(from,to)] ++ dohanoi m using to from



hanoi :: (Num a) => a -> [(a,a)]

hanoi n = dohanoi n 1 3 2