[Haskell-beginners] tower hanoi problem

Mike Meyer mwm at mired.org
Thu Feb 19 07:25:41 UTC 2015


On Thu, Feb 19, 2015 at 1:11 AM, Roelof Wobben <r.wobben at home.nl> wrote:

>  I think I found the answer by some trail and error,
>
> hanoi 1 start _ end = [ (start, end)]
> hanoi n start temp end = hanoi (n-1) start end temp ++ [(start, end)] ++
> hanoi (n-1) temp start end
>
> main = print $ hanoi 3 'a' 'b' 'c'
>

I mentioned earlier that using "hanoi 1 start temp end" instead of
"[(start, end)]" made things read a bit better to me. I figured out why. It
isolates the final representation of a "move" to the base case only. So if
you make that change, you could then write your base case as:

hanoi 1 start _ end = "move 1 disk from " ++ start ++ " to " ++ end ++
".\n"

and the other case still works correctly if you used the "hanoi 1" version.
You'll want to change "print" to "putStr" in main, but it will now print
the list of moves in English instead of Haskell.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150219/237b0121/attachment.html>


More information about the Beginners mailing list