[ Home | About | What's New | Email | Franšais ]

Previous Index Next

Mswlogo downloads

Recursion with the Repeat statement

Recursion may be defined as repetition. For example the procedure

TO SQUARE
FD 50 RT 90 FD 50 RT 90 FD 50 RT 90 FD 50 RT 90
END

will solve the problem of finding a way to draw a square in Logo. On the other hand, the procedure

TO SQUARE
REPEAT 4[RT 90 FD 50]
END

solves the same problem without writing out the directions for drawing each side of the square. Instead, it simply says "draw a side, four times." Incidentally, this is called finite recursion, because we are counting to a finite number. The procedure

TO SQUARE
RT 90 FD 50
SQUARE
END

solves the same problem with an infinite recursion. Recursion is an extremely important characteristic of human problem solving and it is reflected in nearly all computer languages.

 

Using the Repeat statement to draw regular polygons

A Triangle of size 100 units

Repeat 3 [ fd 100 rt 120 ]

A Square of size 100 units

 

Repeat 4 [ fd 100 rt 90 ]

A Pentagon of size 100 units

 

Repeat 5 [ fd 100 rt 72 ]

A Hexagon of size 100 units

 

Repeat 6 [ fd 100 rt 60 ]

A heptagon of size 100 units

 

Repeat 7 [ fd 100 rt 360/7 ]

A Circle of circumference 360 units 
 

Repeat 360 [ fd 1 rt 1 ] 

 


Logo Users Ring
Previous 5 Logo Spoken Here: Ring Home Join Statistics Ring Home
Next 5
Random Site
List Sites
pages viewed since January 19 2002
Previous Index Next