;; Homework 1 solution ;; 1. (5 points) Put the following expressions into Scheme's prefix ;; notation. Type the results into Dr. Scheme's interactions window and ;; see if you get the expected results. ;; 1. 17 ;; 2. 17 * 12 ;; 3. 170 * 5 * 12 ;; 4. 5 * 6 * 7 * 8 ;; Answer: ;; 1. 17 ;; 2. (* 17 12) ;; 3. (* (* 170 5) 12) ;; 4. (* (* (* 5 6) 7) 8) ;; 2. (20 points) Hand evaluate the following Scheme expressions. ;; After you are done, type them into the interactions window in Dr. ;; Scheme to confirm your results. ;; 1. (- (* 3 5) 20) ;; 2. (* pi (* 10 10)) ;; 3. (+ 73 false) ;; 4. (/ 12 0) ;; Answer: ;; 1. (- (* 3 5) 20) ;; = (- 15 20) ;; = -5 ;; Actual output: -5 ;; 2. (* pi (* 10 10)) ;; = (* pi 100) ;; = approx. 314.1592653589793 ;; Actual output: #i314.1592653589793 ;; 3. (+ 73 false) ;; = error: 'false' is not a number. ;; Actual output: +: expects type as 2nd argument, given: false; other arguments were: 73 ;; 4. (/ 12 0) ;; = error: division by zero. ;; Actual output: /: division by zero ;; 3. (15 points) Go to the definitions window and type in the three ;; functions from Lecture 1: owes, dough-area, and pizza-topping-area. ;; Click the execute button. Go to the interactions window, and invoke ;; each function on two different arguments. Hand in your code, your ;; tests and the results. ;; ;; Answer: ;; (Note: the contracts are not technically required for this problem. ;; They will be required in future homework.) ;; owes: number --> number ;; [purpose from lecture] ;; (define (owes p) (* 12 (/ p 8))) ;; dough-area: number --> number ;; [purpose from lecture] ;; (define (dough-area r) (* pi (* r r))) ;; pizza-topping-area: number --> number ;; [purpose from lecture] ;; (define (pizza-topping-area d) (dough-area (- (/ d 2) 1))) ; Examples: ; (Note: These are my examples. You are free to use whatever values you want.) (owes 10) 15 (owes 1) 1.5 (dough-area 5) #i78.53981633974483 (dough-area 0) 0 (pizza-topping-area 12) #i78.53981633974483 (pizza-topping-area 1000000000000000) #i7.85398163397445e+029 ;; 4. (10 points) Write a function Rectangle-area that consumes a height ;; and a width, and produces the area of a rectangle of that size. Be ;; sure to write down the contract and purpose. Test your program on ;; several inputs. Hand evaluate the expression (Rectangle 10 15). ;; Answer: ;; Rectangle-area: number number --> number ;; Return the area of a rectangle with dimensions height x width. ;; [Not required: The units of the return value are of course ;; square-units of whatever units the inputs were measured in.] ;; (define (Rectangle-area height width) (* height width)) ; Tests: ; Again, your test don't have to be exactly what's shown here, but ; you should test a wide range of values. (= (Rectangle-area 2 50) 100) (= (Rectangle-area 5 4) 20) (= (Rectangle-area 20 0) 0) (= (Rectangle-area 1 14) 14) ;; Hand evaluation: ;; (Rectangle-area 10 15) ;; = (* 10 15) ;; = 150