|
Comp210: Principles of Computing and Programming
|
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.
(15 pts total) Functions returning lists
(10 pts) A function substitute-num which takes in a list-of-numbers, plus a "before" number and "after" number and returns a list like the input, except that wherever the input list contains the "before" number, the output list now contains the "after" number.
; (only) one example of calling susbtitute-num: (substitute-num 1901 2001 (cons 6 (cons 1901 (cons 7 (cons 2001 empty))))) (cons 6 (cons 2001 (cons 7 (cons 2001 empty))))
(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.
(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
, ...
; 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.
i
, and creates a rectangle whose northwest corner is
at canvas location (i*band-width, 0),
with width band-width
and height (* i i)
.
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. n
,
and returns a list of shapes: shapes created by the preceding function,
called with n, n-1, n-2, ... 1, 0.
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. (85 pts total)
Last Revised Sunday, 21-Nov-2004 14:11:51 CST
©2004 Stephen Wong and Dung Nguyen