Comp210 Homework 2:
Cond, Structures


Due 01.sep.12 (wed), at the start of class

All problems will require all (applicable) steps from the design recipe, including a template. Before you tackle the homework, remind yourself of our General Advice, Advice on Homeworks (in particular: staple and provide questions), and the Grading Guidelines.


  1. (2pts) Write the function hello, which takes in one of the symbols 'spanish, 'french, or 'pig-latin, and returns the word (symbol) for "hello" in that language. If an unknown language is given to the function, return the universal(?!) english 'hello. (Use the cond-question else.)

    (Yes, you may choose different languages if you prefer, but use at least three. Your template should have one cond-question per case (language-or-other), but the template doesn't yet contain specific answers.)

  2. (2 pts) First, from the "language" menu, choose "add teachpack...", select the teachpack (library) htdp/draw.ss (full name: /home/scheme/plt/103/teachpack/htdp/draw.ss), and (to incorporate this library) press Execute. This library already includes a structure posn with fields x and y, representing a position on a canvas (a drawing window). (This struct is similar to pt_cart from lab.) Test that the teachpack has indeed been loaded, by creating a sample posn or two.

    Write the function translate-posn: posn, num, num --> posn, which takes in a posn, and an x- and y-offset, and returns a new posn which has been offset from the first.

  3. (2pts) Create a structure which to represent a circle. Include a canvas location, radius, and color. (Represent colors with symbols. correction: Represent color as specified if you look up "draw.ss" in helpdesk. It will either be one of the placeholders {RED, YELLOW, WHITE, BLACK, BLUE, GREEN}, or it will be one of the symbols {'red, 'yellow, 'white, 'black, 'blue, 'green}. This line is all you need, as the data definition; you don't need to do any templates for color, since you yourself won't write any functions which process (primarily) colors.) Create example circles, of course. However, you do not need to write a template, since we aren't (yet) writing functions that handle (just) a circle.


  4. (2pts) Create a structure which to represent a rectangle, including a canvas position of its northwest corner, a height and width, and a color.


  5. (3pts) Write down the data definition for a shape, which can be either a circle or a rectangle. Do include examples and a template, since we are about to write shape-processing functions. (Answer for yourself: How many cond branches for a shape?)


  6. (2pts) Write the function perimeter: shape --> number.
    (Note that in the design recipe, you have already given examples and a template for a shape, so you don't need to repeat that.)


  7. (2pts) Write the function in?: shape, posn --> boolean.


  8. (2pts) Write the function translate-shape: shape, number, number --> shape, which returns a new shape offset from the first.


  9. (2pts) Write the function draw-shape: shape --> true, which draws the shape on the canvas. To do this, you will use the other functions from the draw.ss library, as described in helpdesk (search for "draw.ss"), or in Ex. 6.2.1 (if down, try the pictureless version of Ex. 6.2.1).

    In what you turn in, you do not need to show the output of this function, but you should still show the test cases with your code.

  10. (3pts) Write a function my-picture: posn num --> true, which takes in a position and a size, and draws a crude picture of your favorite pet (or whatever). Your picture should comprise at least two simpler shapes. Make sensible use of the arguments.

    Write a function my-picture2: posn --> true, which takes in a location, and draws two copies of my-picture. The two copies should be offset from one another, and may be of different sizes.

    Feel free to play around and include additional parameters to these functions.

    Note on combining draw-effects: Since each of the drawing operations of draw.ss returns true, if you want to draw (say) a circle and a solid line, you can use and: evaluate (and (draw-circle ...) (draw-solid-line ...)). Note that this is one big expression which draws something and then returns true (just like the provided functions).

    Also note that and can actually accept two or more arguments: (and true true false true) = false.