[Haskell-beginners] tower of hanoi problem

Mike Meyer mwm at mired.org
Fri Feb 13 21:54:07 UTC 2015


On Fri, Feb 13, 2015 at 1:39 PM, Roelof Wobben <r.wobben at home.nl> wrote:

>  Hello,
>
> After a short break I try to make the next assignment of the CIS 194
> course.
> I do self-study.
>
> Lets say we have 1 disk with 2 pegs then we have this :
>
>
> type Peg = String.
> type Move = (Peg, Peg)
> Hanoi :: Integer -> Peg -> Peg -> [Move]
>
> So I can do this Hanoi 2 a b
>
> How can I proceed further.
>
> I do not see how I can tell that the disk can move from a to b
> And I do not see what the base case will be . I think when a is empty and
> b has a value.
>

It's a recursion problem. So you tackle it like any other recursion problem:
· Divide your input into two cases: the terminal one that ends the
recursion and the one you recurse on.
· Decide how to handle the terminal case. In this case, you'll return a
list consisting of one move.
· Decide how to divide the non-terminal case into at least two sub-problems
that are all closer to the terminal case by some measurement.
· Write the code that makes the non-terminal calls and combines their
values to create your return value.

In cases where your function returns a list, the combination is almost
always appending one case to the other.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20150213/0ca30abb/attachment.html>


More information about the Beginners mailing list