COMP 210 Homework #4

Fall 2002

Due Mon., Sept. 23. 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. (25 pts total) Finish Homework #3:
    1. (15 pts) Problem 2.
    2. (10 pts) Problem 3d.

  2. (10pts) Hand-evaluation

    Hand-evaluate:

    ;; prod: list-of-nums --> num
    ;; Return the product of a list of numbers.
    ;;
    (define (prod nums)
      (cond [(empty? nums)  1]   ; 1 is the identity element for *.
            [(cons?  nums)  (* (first nums) (prod (rest nums)))]))
    
    (prod (cons 2 (cons 7 empty)))
    
    As usual, your hand-evaluation should include one line per stepper step, and you should indicate (perhaps via underlining or italicizing) on your printout which sub-expression is about to be evaluated.

  3. (10 pts) Natural Numbers

    Write the function my-odd?, which takes a NatNum and tells whether or not it's an odd number. (Include the data definition, examples, template for NatNums, of course.)

    Your function should follow directly from the template. Your code will be based on the fact that a non-zero natural number is odd iff its predecessor isn't.

    For this problem, do not use the built-in functions even?, odd?, remainder, modulo, nor round, ceiling, ...

  4. (20 pts) NatNum in, List out
    1. (0pts) Setup for this problem: Be sure to be using the draw.ss teachpack as in hw02 (Unless you cleared your teachpacks, it's still in use.) Also include the following:
      ; A constant, used for width of various bands, in part (b).
      ;
      (define band-width 20)
      
      ;; --------- Begin data definitions.
      
      ;; A posn is (make-posn num num)
      ;; and is already defined inside draw.ss.
      
      
      ;; A color is: one of {'red, 'yellow, 'white, 'black, 'blue, 'green}.
      
      
      ;;; Note: If you want to use your own structs from hw02
      ;;;       instead of these, that's okay.
      
      (define-struct rectangle (location width height color))
      ;; A rectangle is:
      ;;   (make-rectangle  posn num num color)
      ;; where location refers to the northwest corner.
      
      (define-struct circle (location radius color))
      ;; A circle is:
      ;;   (make-circle posn num color)
      ;; where location refers to the center.
      
      ;; A Shape is either:
      ;;   - a rectangle, or
      ;;   - a circle.
      ;;
      ;; (Note: Since we have already just defined "circle", "rectangle" as data,
      ;;  there's no need to repeat that info.)
      ;; --------- End data definitions.
      
    2. (10 pts) Do one of the following, whichever looks more fun to you:
      • rectangles: Write a simple function that is given a number i, and creates a rectangle whose northwest corner is at canvas location (i*band-width, 0), with width band-width and height (* i i).
      • circles: Write a simple function that is given a number i, and creates a circle centered at (say) canvas location (150,150), with radius (* i band-width), and either the color red or blue, depending on whether i is odd or even.
      Note: you don't need to recur on your input, so this is a simple function.
    3. (10 pts) Write a function which takes in a NatNum n, and returns a list of shapes: shapes created by the preceding function, called with n, n-1, n-2, ... 1, 0.
    4. (0pts) You are not required to write draw-list-of-shapes, and draw the output of your previous function. (It's pretty simple to do, though. Recall that to draw A and draw B, you can combine operations with and (since they all return true.)) If your structs representing circles and rectangles differed from those presented above, that's fine -- use whichever you like, but make sure your data definitions are correct.

(65 pts total)