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.
(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.
(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, ....
; 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.
(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.