CSE 111, Fall 2000

Great Ideas in Computer Science

Lecture Notes #7

Karel the Robot (continued)

12.  Top-Down Design & Stepwise Refinement

a)    A method for creating algorithms
        (i.e., for solving problems algorithmically)
            -    actually, it's a general method for
                  solving any problem!

       *    look at the problem at an abstract level
            ("top" level)

        *    try to solve it at that level, ignoring details
                - i.e., "put off till tomorrow what you
                            don't want to think about today"
                            :-)

        *    look at each step of the abstract solution
              & tackle the details
              ("middle" level)

                -    use TDD & SWR to tackle each detail!
                -    do this till "bottom" level is reached

        i.e., look at each step of the abstract solution
                & consider it as a new problem to be
                solved using TDD & SWR;

                stop when a problem consists of Karel's
                basic instructions

                (in general, stop when each "problem"
                consists of things that you know how
                to do)

e.g., initial situation:    K at (1,1), facing W,
                                   with 8 beepers in bag

        final situation:    K at (1,5), facing W,
                                  all 8 beepers on grid
                                    - 1 each at:
                                        (2,2), (3,2),
                                        (2,3), (3,3),
                                        (2,4), (3,4),
                                        (2,5), (3,5)

    possible solutions:  plant beepers in rows
                                  plant beepers in columns
    try rows, first (exercise:  try columns at home!)

    top-level solution:    get K into position
                                    plant 2 rows
                                    get K into position (again)
                                    plant 2 rows (again)

    2nd-level solution:      get-into-position;
                                      plant-2-rows;
                                      get-into-position;
                                      plant-2-rows

    3rd-level solution =
        top-level solution of "get-into-position":

            turnright;
            move;
            turnright;
            move

     4th-level-solution =
        2nd-level-solution of "get-into-position" =
            top-level solution of "turnright":

                turnleft;
                turnleft;
                turnleft

    Aha!  Now we have the bottom-level solution
            of "get-into-position", since we have that
            whole (part of our original) problem
            expressed in terms of Karel's 5 basic
            instructions.

    3rd-level solution =
        top-level solution of "plant-2-rows":

            plant-first-row;
            get-to-next-position;
            plant-second-row;
            move

   4th-level solution =
        2nd-level solution of "plant-2-rows" =
            top-level solution of "plant-first-row":

                putbeeper;
                move;
                putbeeper

    4th-level solution =
        2nd-level solution of "plant-2-rows" =
            top-level solution of "get-to-next-position":

                turnleft;
                move;
                turnleft

    4th-level solution =
        2nd-level solution of "plant-2-rows" =
            top-level solution of "plant-second-row":

                Aha!  plant-second-row = plant-first-row!

    We can now put it all together:

    beginning-of-program
        define-new-instruction turnright as
            begin
                turnleft;
                turnleft;
                turnleft
            end;
        define-new-instruction get-into-position as
            begin
                turnright;
                move;
                turnright;
                move
            end;
        define-new-instruction plant-first-row as
            begin
                putbeeper;
                move;
                putbeeper
            end;
        define-new-instruction get-to-next-position as
            begin
                turnleft;
                move;
                turnleft
            end;
        define-new-instruction plant-second-row as
            plant-first-row;
        define-new-instruction plant-2-rows as
            begin
                plant-first-row;
                get-to-next-position;
                plant-second-row;
                move
            end;
        beginning-of-execution
            get-into-position;
            plant-2-rows;
            get-into-position;
            plant-2-rows
        end-of-execution
    end-of-program
 

13.  Choice (Selection):  Part I

a)    Want to avoid error-shutoffs

        *    have Karel check where he is before
             executing an instruction that might
             cause an error-shutoff

        *    e.g., instead of just saying "putbeeper",
                    say:

                    IF any-beepers-in-beeper-bag
                        THEN putbeeper

b)    New definition of "instruction"

        (i)    any of the 5 basic instructions isdef an
                instruction

                    NOTES:  1.  "isdef" means:  "is by definition"

                                        2.  I will be defining the term instruction
                                              in pieces; each piece will use the term
                                             being defined.  This is OK in part (i),
                                             since I didn't have to use it; I could have
                                             said, instead, that "move", "turnleft",
                                            "putbeeper", "pickbeeper", and "turnoff"
                                            aredef instructions

            (ii)    any sequence of instructions, separated
                by semicolons (";") and surrounded by
                the reserved words "begin" and "end"
                isdef an instruction.

                    NOTE:  Here, the first occurrence of "instruction"
                                     is being used to define the second occurrence.
                                    But that's OK:  the first time you read this,
                                    it just means that any sequence of "move",
                                    "turnleft", etc., separated by ";" and surrounded
                                    by "begin/end" is an instruction.  Then, once
                                    we have these, we can also say that any
                                    sequence consisting of any "begin...end"
                                    instruction or any of the others, with ";"
                                    and "begin/end" in appropriate places, is
                                    also an instruction, etc.  In other words,
                                    we are building up the notion of an "instruction"
                                    piece by piece.  For the record, such definitions
                                    are said to be "recursive".

TO BE CONTINUED IN LECTURE NOTES 8 ...


Copyright © 2000 by William J. Rapaport (rapaport@cse.buffalo.edu)

file: 111F00/lecturenotes7.20sp00.html