Using Recursion in Procedures

Total Page:16

File Type:pdf, Size:1020Kb

Using Recursion in Procedures

USING RECURSION IN PROCEDURES

When you are creating a procedure, you can use any primitive or procedure as a command in your procedure. This means that in Logo, a procedure can use itself as a command. The ability of a procedure to call itself is called recursion.

In the recursive procedure STAR, the procedure STAR is the third command.

TO STAR FD 60 RT 144 STAR END

In this case, the command says, "Do STAR again" or "Do FD 60 RT 144 again." In other words, REPEAT the commands FD 60 and RT 144 again. So recursion appears to do the same as the REPEAT command. Or does it?

If you teach the turtle the STAR procedure and then give the command STAR, the turtle does make a design that looks like a star, But the turtle continues to do the design over, and over, and over. In fact, to stop the turtle you must CLICK ON THE STOPLIGHT to abort the procedure.

On the other hand, if you teach the turtle the STAR1 procedure, using REPEAT, the turtle again draws a design that looks like a star. But this time the turtle knows when to stop.

TO STAR1 REPEAT 5[FD 60 RT 144] END

Recursion can be used to create procedures that don't stop. Telling the turtle how many times to use the recursive call requires extra commands, usually including variable input. As a result, REPEAT is clearly the better method to use if you know in advance how many times you want the turtle to repeat a procedure.

The recursive call can be controlled with variable input by changing the variable input each time the procedure calls itself. Consider the following procedure:

TO STAR :L FD :L RT 150 STAR ( :L-10) END

Based upon LOGO DISCOVERIES 1984 Creative Publications Page 1 Each time the turtle reaches the third command, the procedure STAR is called, but with a different variable value. For example, suppose you give the command STAR 30. Here are the commands the turtle will receive: STAR 30 FD 30 RT 150 STAR (30-10) (that is, STAR 20) FD 20 RT 150 STAR (20-10) (that is, STAR 10) FD 10 RT 150 STAR (10-10) (that is, STAR 0) FD 0 RT 150 STAR (0-10) (that is, STAR -10) FD -10 RT 150 and so on

One way to make the turtle stop executing the procedure STAR as soon as the variable :L becomes less than zero is to use an IF statement -to test the value of :L. The following procedure will be executed until the value of :L becomes negative, and then the procedure will STOP.

TO STAR :L IF : L< 0 [STOP] FD :L RT 150 STAR ( :L-10) END

So far, all the examples of recursive calls have used tail-end recursion. That means the recursive call is the last command before the END. And it means the recursive call could have been replaced by a REPEAT if you knew how many times to repeat the procedure. The distinction of the recursive call is demonstrated clearly when the call is not the last command, as in the procedure NEST.

TO NEST : S IF : S<10 [STOP] SQUARE : S NEST :S-10 LT 45

Based upon LOGO DISCOVERIES 1984 Creative Publications Page 2 FD :S END

TO SQUARE : S REPEAT 4 [ FD :S RT 90] END

Based upon LOGO DISCOVERIES 1984 Creative Publications Page 3 Let's look at the steps the turtle goes through when given the command NEST 30:

NEST 30 does IF test draws SQUARE 30 and calls NEST 20 does IF test draws SQUARE 20 and calls NEST 10 does IF test draws SQUARE 10 and calls NEST 0 does IF test STOPS and returns control to-,

LT 45 FD 10 END and returns control to-, LT 45 FD 20 END and returns control to-

LT 45 FD 30 END

Based upon LOGO DISCOVERIES 1984 Creative Publications Page 4 Activity 45 Do It Again and Again and ... EXPLORE

Each of the procedures below is recursive. That means each procedure uses itself as a command. Teach the turtle each procedure and then Sketch what the turtle draws. You will need to CLICK ON THE STOPLIGHT to make the turtle stop.

1 . TO STAR 2. TO ZIG FD 60 FD 15 RT 150 RT 120 STAR FD 15 END LT 120 ZIG Sketch: END

Sketch:

3.TO PLAY 4. TO DASH REPEAT 3[FD 20 RT 60] RT 10 LT 180 FD 10 PLAY PU FD 10 PD END DASH END Sketch: Sketch:

5 .TO MYSTERY REPEAT 4[FD 20 LT 30 FD 10 LT 60] PU RT 90 FD 20 LT 90 PD MYSTERY END

Sketch:

Based upon LOGO DISCOVERIES 1984 Creative Publications Page 5 Activity 46 Keep Trying PREDICT

What do you think the turtle would draw if you taught it each of these procedures and then gave it the command TRI 50? Sketch your guess. Then check your prediction by defining the procedure and giving the command. If your prediction was inaccurate, correct your sketch. (To stop the turtle,CLICK ON THE STOPLIGHT.)

1. TO TRI :S Sketch of TRI 50: REPEAT 3[FD :S RT 120] END

2. TO TRI :S Sketch of TRI 50: FD :S RT 120 TRI : S END

3. TO TRI :S Sketch of TRI 50: FD :S RT 115 TRI : S END

4. TO TRI :S Sketch of TRI 50: FD :S RT 125 TRI :S END

Based upon LOGO DISCOVERIES 1984 Creative Publications Page 6 Activity 47 Changing Polygons EXPLORE

Use the test commands to find out about these polygon procedures. Use THE STOPLIGHT TO STOP THE TURTLE.

Procedures Test commands POLYGON 40 90 1 . TO POLYGON :LENGTH :ANGLE POLYGON 10 60 FD : LENGTH POLYGON 20 72 RT :ANGLE POLYGON :LENGTH :ANGLE END

POLYGON 4 10 2. TO POLYGON :N :L POLYGON 5 20. FD :L POLYGON 10 10 RT 360/ :N POLYGON :N :L END

3. TO POLYGON : L :A POLYGON 0 60 FD :L POLYGON 1 45 RT :A POLYGON 5 144 POLYGON ( :L+5) :A END

4. TO POLYGON L :A POLYGON 40 150 FD :L POLYGON 50 100 RT :A POLYGON 70 360 POLYGON :L (:A-5) END

5. TO POLYGON L :A POLYGON 0 150 FD :L POLYGON 1 100 RT :A POLYGON 5 360 POLYGON (:L+5) (:A-5) END

Based upon LOGO DISCOVERIES 1984 Creative Publications Page 7 Based upon LOGO DISCOVERIES 1984 Creative Publications Page 8 Activity 48 Stopping the Turtle EXPLORE

Teach the turtle each procedure. Sketch what the turtle draws when given the special command. Why does the turtle stop?

1. TO ZIG :C Sketch of ZIG 30: IF : C=10 [STOP] FD 20 RT 120 FD 20 LT 120 ZIG ( :C-10) END

2. TO ZAG : C Sketch of ZAG 50: IF : C>100 [STOP] FD 20 RT 150 FD 10 LT 150 ZAG (:C+10) END

3. TO STEP : S Sketch of STEP 10: IF S>40 [STOP] FD S RT 90 FD :S LT 90 STEP ( :S*2) END

4. TO JUMP :H Sketch of JUMP 100: IF : H<30 [STOP] BK :H RT 30 FD :H LT 30 JUMP (:H-20) END

Based upon LOGO DISCOVERIES 1984 Creative Publications Page 9 Stopping a Run-away Turtle DEBUG

Activity 49

These procedures use an IF ... THEN STOP statement to keep the turtle from running away. But the procedures don't stop for all test commands. Fix the procedures so the turtle will stop on all tests.

Run-away procedure commands Corrected procedure

1. TO LINE :L LINE 50 IF : L

2. TO TRY :A TRY 30 IF : A<30 [STOP] FD 30 TRY 0 RT :A TRY 50 TRY ( :A+10) END

3. TO SQUARE :L SQUARE 100 IF : L>100 [STOP] FD :L SQUARE 50 RT 87 SQUARE ( :L-10) SQUARE 10 END

4. TO RECTANGLE : L : W RECTANGLE 50 40 IF : W

Based upon LOGO DISCOVERIES 1984 Creative Publications Page 10 Based upon LOGO DISCOVERIES 1984 Creative Publications Page 11 Activity 50 Is Recursion the Same as REPEAT? EXPLORE

1 Teach the turtle the procedure ROCKET. 2. Change the ROCKET procedure to NEW-ROCKET.

TO ROCKET : Y TO NEW. ROCKET : Y IF : Y>100 [STOP] IF : Y>100 [ STOP] SETUP :Y SETUP :Y RECTANGLE 15 45 RECTANGLE 15 45 RT 90 RT 90 FD 15 FD 15 LT 90 LT 90 RECTANGLE 60 15 RECTANGLE 60 15 FD 60 FD 60 TOP TOP DRAW DRAW ROCKET(:Y+10) NEW.ROCKET(:Y+10) END PRINT [BLASTOFF!] END TO SETUP :Y PU Give the command NEW.ROCKET HT 40 and sketch what the turtle FD :Y draws. PD END Sketch of NEW.ROCKET 40:

TO RECTANGLE :L :W REPEAT 2 [FD :L RT 90 FD :W RT 90] END

TO TOP RT 30 REPEAT 3[FD 15 RT 120] END

Give the command ROCKET 40 and sketch what the turtle draws.

Sketch of ROCKET 40:

Based upon LOGO DISCOVERIES 1984 Creative Publications Page 12 Activity 51 Squaring Off PREDICT

1 Pretend you are the turtle executing the command FIRST.SQUARE 50. Sketch what you think the turtle will draw. Check your prediction by teaching the turtle the procedure.

TO FIRST.SQUARE :SIZE Sketch of FIRST.SQUARE 50: REPEAT 4 [FD : SIZE RT 90] FIRST.SQUARE (:SIZE-10) END

2. Pretend you are the turtle executing the command SQUARE 50. Sketch what you think the turtle will draw. Check your prediction by teaching the turtle the procedure.

TO SQUARE : SIZE Sketch of SQUARE 50: IF : S IZE=O [STOP] REPEAT 4[FD :SIZE RT 90] SQUARE (:SIZE-10) END

3. Pretend you are the turtle executing the command SQUARE.CIRCLE 30. Sketch what you think the turtle will draw. Check your prediction by teaching the turtle the procedure.

TO SQUARE.CIRCLE :SIZE Sketch of SQUARE.CIRCLE 30: IF : SIZE=O [STOP] REPEAT 4[FD : SIZE RT 90] SQUARE.CIRCLE (:SIZE-10) LT 45 FD :SIZE END

Based upon LOGO DISCOVERIES 1984 Creative Publications Page 13

Recommended publications