[Haskell-beginners] hanoi 4 problem

Roelof Wobben r.wobben at home.nl
Thu Feb 19 13:50:10 UTC 2015


Hello,

I try now to make the optional exercise hanoi with 4 pegs,

So I did on paper this :

start               temp1               temp2                  end

3
2
1

first move  :  start -> temp2

start               temp1           temp2 end
2                                              3
1

second move : start -> temp1

start                  temp1       temp2                      end
1                        2                  3

now I tried to make this piece in code

So I did :

hanoi4 1 start _  _ end = "move 1 disk from " ++ [start] ++ " to " ++ 
[end] ++ ".\n"
hanoi4 n start temp1 temp2 end = hanoi4 (n-1) start end temp1 temp2 ++ 
hanoi4 1 start end temp1 temp2 ++ hanoi4 (n-1) start  temp1 end temp2

because on the first step   start must be  start and  end must be temp1
and on the second step start must be start and end must be temp2

but when I run in I see this output :

move 1 disk from a to b.
move 1 disk from a to b.
move 1 disk from a to b.
move 1 disk from a to c.
move 1 disk from a to d.
move 1 disk from a to d.
move 1 disk from a to d.

Can anyone explain or let me see where my thinking took the wrong turn ?

Roelof





More information about the Beginners mailing list