The Logo language, which is used to teach programming to children, gives commands to a ``turtle'' -- a mechanical device that moves and can draw with a pen that it carries. Some versions of Logo have a mechanical turtle; other versions simulate it on a display screen.

A language similar to Logo can easily be developed within Lisp.

- Define global variables
`*x*`,`*y*`,`*angle*`, and`*pen*`to represent the current position of the turtle, the angle (in degrees) at which it is facing, and whether its pen is down and drawing (`#t`) or up and not drawing (`#f`). Choose appropriate initial values for the variables. - Write a function
`(radians theta)`that converts an angle`theta`expressed in degrees to an equivalent angle expressed in radians.*2 pi radians = 360 degrees*. The trigonometric functions in Scheme expect arguments in radians. - Write functions
`(moveto x y)`and`(turnto theta)`that will set the current location and heading of the turtle. We will consider a heading of`0`to mean that the turtle is facing to the right along the horizontal x axis. Write functions`(penup)`and`(pendown)`to set the pen state. Use`set!`to update the global variables appropriately. Although these actions could be achieved by letting the user set the values of global variables, it is preferable to define functions for this interface so that the internal implementation is hidden from the user; this is called*information hiding*. - Write functions
`(left n)`and`(right n)`that will change the heading of the turtle by`n`degrees. Use`set!`to update the global variable appropriately. - Write functions
`(forward r)`and`(back r)`that will move the turtle forward or backward by`r`units along its current direction. Update the global variables appropriately after the movement. If the pen is drawing, a line should be drawn on the screen from the current turtle position to its new position using the graphics functions. (Is there a clever way to write`back`in terms of`forward`?) - Write a function
`(rectangle width height)`that uses the turtle graphics functions to draw a rectangle. Note that the heading of the turtle should be the same after drawing the rectangle as it was before. Demonstrate that your program will draw a rectangle at various angles, depending on the initial angle of the turtle. - Write a function
`(move-turn lng theta n)`that will move forward a distance`lng`, then turn left an angle`theta`degrees, and repeat this operation`n`times. Try`(move-turn 50 120 3)`and`(move-turn 1 1 360)`. - Write a function
`(polygon n r)`that draws a polygon with`n`sides that would fit inside a circle of radius`r`. The first line of the polygon should be drawn from the turtle's current position at the turtle's current heading. After drawing, the turtle should be back at its original position and heading. (Hint: It is always legal to use any function you have written previously as a subroutine.) Leave the turtle's heading afterwards the same as it was at the beginning. Try`(dotimes (i 10) (polygon 5 50) (left 36))`. - Write a function
`(polypoly n r m s)`that draws polygons with`n`sides that would fit inside a circle of radius`r`; these polygons, in turn, are drawn at the vertices of an imaginary polygon with`m`sides that would fit inside a circle of radius`s`. - Write function(s)
`(dashline line space lng)`that draw a dashed line of length`lng`along the turtle's current direction. The line should begin with a drawn line of length`line`followed by a blank region of length`space`, etc. until a dashed line of exactly the length specified by`lng`has been drawn. If drawing the last segment of the line would go too far, shorten it to the right amount. - Write a function
`(archimedes a degrees)`to draw a spiral of Archimedes: a curve whose equation in polar coordinates is*r = a * theta*, where*theta*is in radians; such a curve is formed as a string that is wrapped around a cylinder of radius*a*is unwound. As the string is unwrapped by an amount*dtheta*(*dtheta*denotes a small change to*theta*, expressed in radians), the radius*r*at which the curve is drawn increases by*a * dtheta*; the incremental length of the arc is*r * dtheta*. To draw the spiral, start with a drawing radius of`0`; move the turtle forward by the arc length, then turn and increase the drawing radius. Use an auxiliary recursive function and continue drawing until the angle equals the specified number of`degrees`. - Modify your
`archimedes`to produce another function to draw a geometric spiral,`(spiral a degrees)`. Instead of adding a constant amount to the radius each time, the radius should be multiplied by a constant amount (slightly more than 1). Start with a radius of 10 and multiply the radius by an amount`a`per revolution (hint: if there are`n`angle steps per revolution (e.g.,*n = 360*for*1 degree*steps), the multiplier should be`(expt a (/ 1 n))`). Important: the starting radius must be nonzero for the geometric spiral.