
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

What you did recursively is ...
- Move (n - 1) from peg1 to peg3
- Move 1 from peg1 to peg3
- Move (n - 1) from peg1 to peg3
.. which is not what you want. Keep thinking, you will get it.
Some things to consider:
- Distribute the discs on two intermediates.
- Keep track of the order in which discs are assembled on the
intermediates.
- Move 1 disc from peg1 to peg4.
- Then, move the distributed discs back in the reverse order from how
you put them there.
By keeping track, I mean keep track of which peg is filled first.
Hope this helps.
On 19 February 2015 at 19:20, Roelof Wobben
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
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-- Regards Sumit Sahrawat
participants (2)
-
Roelof Wobben
-
Sumit Sahrawat, Maths & Computing, IIT (BHU)