Handout 8A

The purpose of this exercise is to learn how the MAKE command assigns values to variables in LOGO. One use of this command is to make procedures state transparent (see p. 206). We will find many other uses for MAKE through the course of the semester.

The MAKE command assigns a value to a variable in LOGO. So far, we have only been able to choose values for the arguments of a procedure or command. MAKE lets us assign values to any variable we like. For instance, the following command assigns the value 50 to the variable :LENGTH.

MAKE "LENGTH 50
PRINT :LENGTH
MAKE can also be used to change the value of a variable:
REPEAT 4 [FORWARD :LENGTH RIGHT 90]    ; See that :LENGTH is 50
MAKE "LENGTH 100                       ; Change the value of :LENGTH
REPEAT 4 [FORWARD :LENGTH RIGHT 90]    ; See that :LENGTH is now 100
Use the MAKE command to set the value of :LENGTH to 75 and draw a square with that side length. Experiment with MAKE until you're comfortable using it.


One very important use of MAKE is to "remember" the turtle's state so that procedures can be made state transparent even when it is not possible to know exactly what the turtle's state will be when the procedure finishes running. Can you see how the commands START and RESTART defined below would do this?

TO START                 ; Procedure to record starting turtle state
  MAKE "XSTART XCOR
  MAKE "YSTART YCOR
  MAKE "HSTART HEADING
  MAKE "PCSTART PENCOLOR
END

TO RESTART               ; Procedure to restore starting state
  PENUP
  SETX :XSTART
  SETY :YSTART
  SETHEADING :HSTART
  SETPC :PCSTART
  PENDOWN
END
Try using these commands; run START, move the turtle on the screen, then run RESTART. Move the turtle some more, run START again, move again, then RESTART. Can you see how these commands can help you create state transparent procedures?

Below is a simple example that uses START and RESTART to create a state transparent procedure that draws a randomly placed arrow on the screen.

TO RANDOMARROW
  START                   ; Record starting turtle state

  SETHEADING (RANDOM 360) ; Choose a random direction
  PENUP
  FORWARD (RANDOM 100)    ; Move forward a random distance
  PENDOWN
  FORWARD 50 RIGHT 150 FORWARD 15 BACK 15 RIGHT 60 FORWARD 15 ; Draw arrow

  RESTART                 ; Restore starting state
END
The line of code that draws the arrow is not state transparent; it doesn't have to be! Although the turtle is in some randomly chosen position when the turtle is done drawing, it is always back at its starting position at the end of the procedure. Try running this procedure with different starting positions, headings, and colors.


Suppose we had two variables :A and :B and wanted to exchange their values. This problem comes up frequently when sorting lists of numbers. A first attempt at this might look like:

TO SWAP1 :NUMBER1 :NUMBER2
  PRINT (SENTENCE [:NUMBER1 is] :NUMBER1 [and :NUMBER2 is] :NUMBER2)
  MAKE "NUMBER1 :NUMBER2
  MAKE "NUMBER2 :NUMBER1
  PRINT (SENTENCE [:NUMBER1 is] :NUMBER1 [and :NUMBER2 is] :NUMBER2)
END
Run this program. Why doesn't it do what we want it to? Can you fix it?


If you have extra time, study the GARDEN procedure below. It uses START and RESTART to draw a randomly generated garden of :NUMFLOWERS flowers of a specified size in the rectangle bounded by :XMIN, :XMAX, :YMIN and :YMAX. Compare this to your own GARDEN procedure. Read through the program, paying particular attention to the way MAKE is used to "learn" the pen color and turtle position in START and resest it in RESTART. Also, notice how the variable :NUMBER is used to control the number of times the procedure DRAWFLOWERS is called.

Run the program a few times, making gardens of different shapes and sizes and with different numbers of flowers.

TO FLOWER :SIZE
  REPEAT 5 [FORWARD :SIZE RIGHT 144]
END


TO DRAWFLOWERS :NUMBER :SIZE :XMIN :XMAX :YMIN :YMAX
  IF (:NUMBER = 0) [STOP]                 ; Check whether done.

  PENUP
  SETPC (1 + RANDOM 15)                   ; Choose random color.
  SETX (:XMIN + (RANDOM (:XMAX - :XMIN))) ; Choose random position.
  SETY (:YMIN + (RANDOM (:YMAX - :YMIN)))
  PENDOWN

  FLOWER :SIZE                            ; Draw a flower.

  DRAWFLOWERS (:NUMBER - 1) :SIZE :XMIN :XMAX :YMIN :YMAX
                                          ; Repeat.
END


TO GARDEN :NUMBER :SIZE :XMIN :XMAX :YMIN :YMAX
  START

  DRAWFLOWERS :NUMBER :SIZE :XMIN :XMAX :YMIN :YMAX

  RESTART 
END 
When you understand how the GARDEN procedure works, start making changes to it! Some things you might try are: randomly changing the turtle heading so that not all flowers have the same orientation, rewriting the FLOWER procedure, changing the range of colors used, or randomly selecting a flower size.


Mtht420