Hanoi Tower programming challenge...

Alex Tweedly alex at tweedly.net
Thu Jul 19 19:46:49 EDT 2012


Here I go - replying to my own posting.

Silly me
Q:  What happens when a function reaches the "end" statement ?
  A: It returns "empty"

So we are basically down to 3 lines !!
(with even more line wraps)

> on mouseUp
>    ask "How many disks are there?"
>    put tower (it , "A", "B" , "C") into field 1
> end mouseUp
>
>
> function tower N, start, destination, spare
>     if N > 0 then  return tower (N - 1, start, spare, destination) &  
> "Move " & N & " from "& start & " to "& destination & cr & tower (N - 
> 1, spare, destination, start)
> end tower
-- Alex.



On 20/07/2012 00:18, Alex Tweedly wrote:
> Sorry if this is a duplicate - I think the original got caught because 
> I sent it from the wrong address  ....  you can save 2 lines by using 
> a function rather than a handler (watch for line wraps)
>
> on mouseUp
>    ask "How many disks are there?"
>    put tower (it , "A", "B" , "C") into field 1
> end mouseUp
>
>
> function tower N, start, destination, spare
>     if N = 0 then return ""
>     return tower (N - 1, start, spare, destination) &  "Move " & N & " 
> from "& start & " to "& destination & cr & tower (N - 1, spare, 
> destination, start)
> end tower
>
> -- Alex
>
> On 19/07/2012 11:59, Jim Hurley wrote:
>>
>> How about this? I think this is the classic recursive solution.
>>
>> Jim Hurley
>>
>>
>> on mouseUp
>>     ask "How many disks are there?"
>>     tower it , "A", "B" , "C"
>> end mouseUp
>>
>> on tower N, start, destination, spare
>>    if N = 0 then exit tower
>>    tower N - 1, start, spare, destination
>>    put "Move "&  N&  " from"&  start&  " to"&  destination&  cr after 
>> field 1
>>    tower N - 1, spare, destination, start
>> end tower
>> _______________________________________________
>> use-livecode mailing list
>> use-livecode at lists.runrev.com
>> Please visit this url to subscribe, unsubscribe and manage your 
>> subscription preferences:
>> http://lists.runrev.com/mailman/listinfo/use-livecode
>
>
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your 
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode





More information about the use-livecode mailing list