Goal: Use recursion to limit a spiral´s size.
Vocabulary: if, > (greater than sign), stop
to spiral :length
if :length > howbig [stop]
fd :length rt angle
spiral :length + howtight
When we call the spiral
procedure from within the spiral
procedure, that tells the computer to start running the procedure all over again. (A procedure which calls its own name is called a recursive procedure
.) The computer will keep circling back to the beginning forever unless we tell it when to stop.
- Instead of setting the number of turns as in Spirals 4, we make a howbig slider to set a limit on the length of the lines.
- In the spiral procedure, if :length > howbig [stop] means, "If the value of the length variable is greater than the number on the howbig slider, stop the procedure."
Every time the computer runs the spiral procedure, it checks to see if the if command is true. If it is true, the procedure will stop. If it is false, the procedure will keep running.
- Spiral :length + howtight means, "Start running the spiral procedure again, but increase the value of length by the number on the howtight slider."
When the recursive call takes place at the end of a procedure, it is called tail-end recursion.
- You should always save your work before you test out a recursive procedure, in case it doesn't stop as you expect. Also, you can always interrupt a process by selecting "stop all" from the pull-down Edit menu.
Try different settings on the angle, howtight, and howbig sliders.
Can you set the sliders so that the turtle will draw a spiral that reaches the top and bottom of the project page without wrapping?
On Your Own - Project Ideas
Can you revise the procedure so that the spiral starts out drawing large lines that gradually shrink?