COMP 210, Spring 2001
Homework 4 : NatNums, FamTrees
Due Sept.24 (mon), in class, or DH3103 by 17:00.

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) 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 *.
            [else  (* (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 on your printout which expression is about to be evaluated.

  2. (2pts) 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; you do not need any helper function for this. 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, nor round, ceiling, ....

  3. (3pts) 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.
      
      
      ;; v.103:   A color is: one of { RED,  YELLOW,  WHITE,  BLACK,  BLUE,  GREEN}.
      ;; v.103.5: A color is: one of {'red, 'yellow, 'white, 'black, 'blue, 'green}.
      ;;;; These different versions of drscheme have slightly different versions 
      ;;;; of draw.ss; in what you submit, keep only the pertinent def'n.
      
      
      ;;; 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. 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 position ((* i band-width),0), has 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 position (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. 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.))
  4. (3pts) Functions Processing Family Trees

    Using the data definition of FamTree from lecture, write a function size-all: FamTree --> num , which counts the total number of nodes in a FamTree -- that is, the number of children and 'unknowns.