Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: tower of hanoi problem (Roelof Wobben)
2. Re: tower of hanoi problem (YCH)
3. Re: tower of hanoi problem (Mike Meyer)
4. Re: tower of hanoi problem (Julian Birch)
----------------------------------------------------------------------
Message: 1
Date: Sat, 14 Feb 2015 13:43:18 +0100
From: Roelof Wobben <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] tower of hanoi problem
Message-ID: <[email protected]>
Content-Type: text/plain; charset=windows-1252; format=flowed
Hello,
I try to answer your questions. Maybe then it is clear where My thinking
gets the wrong turn.
>
> ? Divide your input into two cases: the terminal one that ends the
> recursion and the one you recurse on.
I think the terminal one is when there is no moves anymore so when a and
b are empty and c holds all the disk.
> ? Decide how to handle the terminal case. In this case, you'll return
> a list consisting of one move.
There is a problem. I thought I must return all the moves then.
> ? 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.
two subproblems ? I can only think about one and that is looking what
the next move can be.
> ? Write the code that makes the non-terminal calls and combines their
> values to create your return value.
Oke, when I have the right answers to the former questions, I can think
about how to solve this one.
>
> In cases where your function returns a list, the combination is almost
> always appending one case to the other.
I also think so , so it will be old case ++ new case because otherwise
it will display in the wrong order.
Roelof
------------------------------
Message: 2
Date: Sat, 14 Feb 2015 22:16:52 +0900
From: YCH <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] tower of hanoi problem
Message-ID:
<captm4gkna-u+jjxj+5f_rbztp6hnjrzrbjyuvm4kkwovvvw...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
On Sat, Feb 14, 2015 at 4:39 AM, Roelof Wobben <[email protected]> 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
Should it be,
hanoi :: Integer -> Peg -> Peg -> Peg -> [Move]
?
So,
hanoi n a b c
hanoi 2 "a" "b" "c"
- You have n disks at 'a' peg in right order(bigger is on bottom).
- n should be >= 2.
- You have to move all disk from 'a' to 'b'.
- You can use 'c' as your temporary place.
I've done this as,
- write 'hanoi 2 "a" "b" "c"
- write 'hanoi 3 "a" "b" "c"
- write 'hanoi 4 "a" "b" "c"
- find some common pattern
>I do not see how I can tell that the disk can move from a to b
Output of hanoi is 'move plan'. It does not actually do somthing.
[ ("a", "b")
, ("a", "c")
]
means,
- move disk on top of "a" peg to "b"
- move disk on top of "b" peg to "c"
------------------------------
Message: 3
Date: Sat, 14 Feb 2015 09:24:17 -0600
From: Mike Meyer <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] tower of hanoi problem
Message-ID:
<CAD=7u2dmgt7pdklqhskpqpmk0+ogpkfhbquqd+mh2forojg...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Sat, Feb 14, 2015 at 6:43 AM, Roelof Wobben <[email protected]> wrote:
> Hello,
>
> I try to answer your questions. Maybe then it is clear where My thinking
> gets the wrong turn.
>
>>
>> ? Divide your input into two cases: the terminal one that ends the
>> recursion and the one you recurse on.
>>
>
> I think the terminal one is when there is no moves anymore so when a and b
> are empty and c holds all the disk.
>
I'd say the terminal one is one move, not no moves. I even gave it away
with the next comment:
? Decide how to handle the terminal case. In this case, you'll return a
>> list consisting of one move.
>
> There is a problem. I thought I must return all the moves then.
>
Ah, I think we miscommunciated about what "terminal" means. I meant the
case that terminates the recursion, not the problem. That result will be
combined with the values returned by other calls to create the ultimate
return value having all the moves.
So when hanoi is called with only 1 disk to move, you just return the list
consisting of that move. If it's called with more than 1 disk, you have to
make multiple recursive calls with fewer disks than it was called with. I
don't think you can do this without special handling for the single disk
case, so you might as well make that one your terminating case. Handling
the no-disk case would be part of the initial error checking.
> ? 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.
>
> two subproblems ? I can only think about one and that is looking what the
> next move can be.
>
There's gotta be at least two subproblems, because you have to create
smaller problems for the recursive calls. If you can make the problem
smaller without creating two subpropblems - well, then your problem can
just goes away!
For hanoi, you don't need to worry about what the next move will be. Assume
that you have a working hanoi routine that can handle fewer disks that you
currently have. How can you use that to solve your current problem? It
should be obvious that you have to move some number less than all of them
to one peg, then move the rest to the other peg, and finally move the first
stack you moved on top of the second stack. So you need to figure out how
many to move and where to move them to so you don't make any illegal moves
in the process.
> ? Write the code that makes the non-terminal calls and combines their
>> values to create your return value.
>
> Oke, when I have the right answers to the former questions, I can think
> about how to solve this one.
Yup.
> In cases where your function returns a list, the combination is almost
>> always appending one case to the other.
>
> I also think so , so it will be old case ++ new case because otherwise it
> will display in the wrong order.
>
Well, the solution I outlined has more than one case to deal with, as you
have to make three recursive calls.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20150214/4b357ebe/attachment-0001.html>
------------------------------
Message: 4
Date: Sat, 14 Feb 2015 15:39:36 +0000
From: Julian Birch <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] tower of hanoi problem
Message-ID:
<CAB0TuzA83Y=bxudou8wj7mffvrvfcan9hdq3b+qtuge8qou...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Slightly off-topic, but you may be interesting in my post about the
Fox/Goose/Corn problem.
http://www.colourcoding.net/blog/archive/2015/01/03/fox-goose-corn-in-haskell-for-clojure-programmers.aspx
It's a full Haskell solution from beginning to end, and explains the steps
as it goes along, and it's a pretty similar problem to Hanoi.
J
On 14 February 2015 at 15:24, Mike Meyer <[email protected]> wrote:
> On Sat, Feb 14, 2015 at 6:43 AM, Roelof Wobben <[email protected]> wrote:
>
>> Hello,
>>
>> I try to answer your questions. Maybe then it is clear where My thinking
>> gets the wrong turn.
>>
>>>
>>> ? Divide your input into two cases: the terminal one that ends the
>>> recursion and the one you recurse on.
>>>
>>
>> I think the terminal one is when there is no moves anymore so when a and
>> b are empty and c holds all the disk.
>>
>
> I'd say the terminal one is one move, not no moves. I even gave it away
> with the next comment:
>
> ? Decide how to handle the terminal case. In this case, you'll return a
>>> list consisting of one move.
>>
>> There is a problem. I thought I must return all the moves then.
>>
>
> Ah, I think we miscommunciated about what "terminal" means. I meant the
> case that terminates the recursion, not the problem. That result will be
> combined with the values returned by other calls to create the ultimate
> return value having all the moves.
>
> So when hanoi is called with only 1 disk to move, you just return the list
> consisting of that move. If it's called with more than 1 disk, you have to
> make multiple recursive calls with fewer disks than it was called with. I
> don't think you can do this without special handling for the single disk
> case, so you might as well make that one your terminating case. Handling
> the no-disk case would be part of the initial error checking.
>
>
>> ? 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.
>>
>> two subproblems ? I can only think about one and that is looking what
>> the next move can be.
>>
>
> There's gotta be at least two subproblems, because you have to create
> smaller problems for the recursive calls. If you can make the problem
> smaller without creating two subpropblems - well, then your problem can
> just goes away!
>
> For hanoi, you don't need to worry about what the next move will be.
> Assume that you have a working hanoi routine that can handle fewer disks
> that you currently have. How can you use that to solve your current
> problem? It should be obvious that you have to move some number less than
> all of them to one peg, then move the rest to the other peg, and finally
> move the first stack you moved on top of the second stack. So you need to
> figure out how many to move and where to move them to so you don't make any
> illegal moves in the process.
>
>
>> ? Write the code that makes the non-terminal calls and combines their
>>> values to create your return value.
>>
>> Oke, when I have the right answers to the former questions, I can think
>> about how to solve this one.
>
>
> Yup.
>
>
>> In cases where your function returns a list, the combination is almost
>>> always appending one case to the other.
>>
>> I also think so , so it will be old case ++ new case because otherwise it
>> will display in the wrong order.
>>
>
> Well, the solution I outlined has more than one case to deal with, as you
> have to make three recursive calls.
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20150214/b19b2bfa/attachment.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 80, Issue 25
*****************************************