Comp210: Principles of Computing and Programming
Fall 2004 -- Homework #2   


Read Sections 5-8 in the H.T.D.P. book.

Before you start the homework, you should remind yourself of our General Advice, Advice on Homeworks, and Grading Guidelines. All are available from the class web site ( http://www.owlnet.rice.edu/~comp210) .

You should turn in one copy of the homework for your team (not one per person) .
Clearly print the names of both team members inside the homework. Use the proper file naming convention!

Use the drop-off link given on the class homepage.

Be sure to completely follow ALL the steps of the Design Recipe and write down any templates that are determined by your data definitions!

 

  1. (5 pts) Write a function that calculates the per-person entrance fee to that oh-so-fun amusement park, Schemeland. There should be different prices for a single person, and groups of 2-4, 5-10 and more than 10.

  2. (45 pts total) First, from the "Language" menu, choose "Add Teachpack...", select the teachpack (library) 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). (As in Section 6.1 of the text.) Test that the teachpack has indeed been loaded, by creating a sample posn or two.

    1. (5 pts) 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.

    2. (5pts) Create a structure which to represent a circle. Include a canvas location, radius, and Color. We'll represent Colors with elements of the set {'red, 'yellow, 'white, 'black, 'blue, 'green}.

      Technically we should have a data definition for Color, so copy the previous sentence in your code. You don't need any examples-of-data for Color (they're already listed) nor any templates for Color (you're not writing any functions which will process Colors).

      Do create examples-of-data for Circles, of course. However, you do not need to write a template, since we aren't (yet) writing functions that handle (just) a circle.

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

    4. (5 pts) 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?)

    5. (5 pts) Write the function perimeter: shape --> number.

      Note that in the design recipe, you have already given examples and a template for a Shape, so don't repeat that. (Though presumably you'll copy/paste the template to start writing your function.) You're not making any new datatype declarations, so you won't have any templates or examples-of-data.
      Remember that (define-struct myStruct (...)) automagically defines not just a constructor and selectors, but also the predicate function myStruct?: ANY --> boolean.

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

      Again you'll presumably copy/paste the Shape template from part (d); don't make any new templates since we're not defining a new data type.

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

    8. (5 pts) 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.

    9. (5 pts) 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.

(50 points total)

 


Last Revised Sunday, 21-Nov-2004 14:10:38 CST

©2004 Stephen Wong and Dung Nguyen