[Edu-sig] GvR question

André Roberge andre.roberge at gmail.com
Wed May 4 06:46:11 CEST 2005


Dan Schellenberg wrote:
[snip]
> 
> On lesson 17 http://gvr.sourceforge.net/lessons/rfrank/step17/  I have 
> been unable to contrive a solution that I think my students would 
> understand at this stage.  The problem is very straightforward, but the 
> difficulty comes in assigning a proper while loop to the code block that 
> defines how long Guido continues to pick up garbage (ie. none of the 
> built in conditionals provide a good boolean for this problem, though 
> right_is_clear is workable, just not eloquent).  I have solved the 
> problem using the aforementioned right_is_clear conditional in the while 
> loop (see first example below), and have also solved it (much more 
> eloquently in my opinion) using recursion.  However, I would like to 
> have a more eloquent solution without having to use recursion, as I am 
> not sure that I want to introduce that concept at this stage.  Below are 
> my two solutions.  Anyone have an idea on how to improve the readability 
> of these solutions for the students?

Here's my non-recursive solution in rur-ple, which could be easily 
translated in GvR.  Actually, I tried to keep your structure intact as 
much as possible.  It does not use the right_is_clear conditional.


# definitions

def turn_right():
     repeat(turn_left, 3)

def turn_around():
     turn_left()
     turn_left()

def facing_south():   # not built-in yet in rur-ple;
                       # hence need to use return
     turn_around()
     answer = facing_North()
     turn_around()
     return answer

def go_south_west_corner():
     while not facing_south():
         turn_left()
     while front_is_clear():
         move()
     turn_right()
     while front_is_clear():
         move()
     turn_right()  # thus facing north

def go_north_east_corner():
     while not facing_North():
         turn_left()
     while front_is_clear():
         move()
     turn_right()
     while front_is_clear():
         move()

# the following definition is modified from yours

def pickup_column_and_come_back(): # normally facing north
     while front_is_clear():
         while next_to_a_beeper():
             pick_beeper()
         move()
     turn_around()                  # now facing south
     while front_is_clear():
         move()
     turn_left()                    # now facing east, perhaps ending
     if front_is_clear():
         move()
         turn_left()                # ends next column, face North

# end of definitions
# =================
# begin algorithm per se

go_south_west_corner()

while facing_North():
     pickup_column_and_come_back()
     # note: when reaching east wall, will not face north

# exits at south-east corner, facing east

turn_left()
while front_is_clear():
     move()

while carries_beepers():
     put_beeper()

go_south_west_corner()
turn_off()
---------------------------------------------------

André



More information about the Edu-sig mailing list