;; Nicholas Tu (partners with Xinran Zhu, but we worked separately ;; this weekend because I was away) ;; HW 1 ;; ***************************************************************** ;; 2.2.3 ;; Contract: triangle : number number -> number ;; ;; Purpose: to compute the area of a triangle when given a base and ;; perpendicular height ;; ;; Examples: (triangle 3 4) should produce 6 ;; (triangle 5 1) should produce 2.5 ;; ;; Definition: (define (triangle base height) (* 1/2 (* base height))) ;; ;; Tests: (triangle 3 4) (triangle 5 1) ;; Expected values are expressed directly as follows: (= (triangle 3 4) 6) (= (triangle 5 1) 2.5) ;; ****************************************************************** ;; 2.2.5 (part 2 only) ;; Contract: 3 equations: number -> number ;; ;; Purpose: to formulate three expressions as programs ;; f(n) = n^2 + 10 ;; g(n) = (1/2) ยท n^2 + 20 ;; h(n) = 2 - (1/n) ;; ;; Examples: (f 2) should produce 14 ;; (f 9) should produce 91 ;; (g 2) should produce 22 ;; (g 9) should produce 60.5 ;; (h 2) should produce 1.5 ;; (h 9) should produce 1.88888... ;; ;; Definitions: (define (f n) (+ (* n n) 10)) (define (g n) (+ (* .5 (* n n)) 20)) (define (h n) (- 2 (/ 1 n))) ;; ;; Tests: (f 2) (f 9) (g 2) (g 9) (h 2) (h 9) ;; Expected values are expressed directly as follows: (= (f 2) 14) (= (f 9) 91) (= (g 2) 22) (= (g 9) 60.5) (= (h 2) 1.5) (= (h 9) (+ 1 8/9)) ;; ****************************************************************** ;; 2.3.1 ;; Contract: tax: num -> num ;; netpay: num -> num ;; ;; Purpose: - to determine the tax on an employee's gross pay, given ;; the payment amount and an assumed tax rate of 15% ;; - to determine the netpay of an employee after tax, ;; given how many hours worked and an assumed wage rate ;; of $12 an hour ;; ;; Examples: (tax 100) should produce 15 ;; (tax 500) should produce 75 ;; (netpay 5) should produce 51 ;; (netpay 8) should produce 81.6 ;; ;; Definitions: (define (tax n) (* n .15)) (define (wage h) (* 12 h)) ;;(given in reading) (define (netpay n) (- (wage n) (tax (wage n)))) ;; ;; Tests: (tax 100) (tax 500) (netpay 5) (netpay 8) ;; Expected values are expressed directly as follows: (= (tax 100) 15) (= (tax 500) 75) (= (netpay 5) 51) (= (netpay 8) 81.6) ;; ****************************************************************** ;; 3.1.4 ;; Contract: profit: num -> num ;; revenue: num -> num ;; cost: num -> num ;; attendees: num -> num ;; ;; Purpose: to compute the profit as the difference between revenue ;; and costs at some given ticket-price ;; to compute the revenue, given ticket-price ;; to compute the costs, given ticket-price ;; to compute the number of attendees, given ticket-price ;; ;; Examples: (cost 5) should produce 180 ;; (cost 3) should produce 630 ;; (profit 5) should produce 420 ;; (revenue 4) should produce 1080 ;; (attendees 5) should produce 120 ;; ;; Definitions: (define (profit ticket-price) (- (revenue ticket-price) (cost ticket-price))) (define (revenue ticket-price) (* (attendees ticket-price) ticket-price)) (define (cost ticket-price) (* 1.5 (attendees ticket-price))) (define (attendees ticket-price) (+ 120 (* (/ 15 .10) (- 5.00 ticket-price)))) ;; ;; Tests: (cost 5) (cost 3) (profit 5) (revenue 4) (attendees 5) ;; Expected values are expressed directly as follows: (= (cost 5) 180) (= (cost 3) 630) (= (profit 5) 420) (= (revenue 4) 1080) (= (attendees 5) 120) ;; ;; ****************************************************************** ;; 3.3.2 ;; Contract: volume-cylinder: num num -> num ;; base-area: num -> num ;; ;; Purpose: This program consumes the radius of a cylinder's base ;; disk and its height; it computes the volume of the ;; cylinder. Uses PI = 3.14. ;; ;; Examples: (volume-cylinder 5 2) should return 157 ;; (volume-cylinder 2 4) should return 50.24 ;; (volume-cylinder 3 3) should return 84.78 ;; (volume-cylinder 1 5) should return 15.7 ;; (base-area 5) should return 78.5 ;; ;; Definition: (define PI 3.14) (define (base-area r) (* PI (* r r))) (define (volume-cylinder radius height) (* (base-area radius) height)) ;; ;; Tests: (volume-cylinder 5 2) (volume-cylinder 2 4) (volume-cylinder 3 3) (volume-cylinder 1 5) (base-area 5) ;; Expected values are expressed directly as follows: (= (volume-cylinder 5 2) 157) (= (volume-cylinder 2 4) 50.24) (= (volume-cylinder 3 3) 84.78) (= (volume-cylinder 1 5) 15.7) (= (base-area 5) 78.5) ;; ;; ****************************************************************** ;; 3.3.3 ;; Contract: area-cylinder: num num -> num ;; base-perimeter: num -> num ;; (reusing base-area from 3.3.2) ;; ;; Purpose: The program consumes the radius of the cylinder's base ;; disk and its height. Its result is the surface area of ;; the cylinder. ;; ;; Examples: (area-cylinder 5 2) should return 219.8 ;; (area-cylinder 2 4) should return 75.36 ;; (area-cylinder 3 3) should return 113.04 ;; (area-cylinder 1 5) should return 37.68 ;; (base-perimeter 5) should return 31.4 ;; ;; Definition: (PI and base-area definitions reused from 3.3.2) (define (base-perimeter r) (* 2 (* PI r))) (define (area-cylinder radius height) (+ (* 2 (base-area radius)) (* (base-perimeter radius) height))) ;; ;; Tests: (area-cylinder 5 2) (area-cylinder 2 4) (area-cylinder 3 3) (area-cylinder 1 5) (base-perimeter 5) ;; Expected values are expressed directly as follows: (= (area-cylinder 5 2) 219.8) (= (area-cylinder 2 4) 75.36) (= (area-cylinder 3 3) 113.04) (= (area-cylinder 1 5) 37.68) (= (base-perimeter 5) 31.4) ;; ;; ****************************************************************** ;; 3.3.4 ;; Contract: base-outer-area: num num -> num ;; inner-area: num num -> num ;; outer-area: num num num -> num ;; area-pipe: num num num -> num ;; ;; Purpose: computes the surface area of a pipe, which is an open ;; cylinder. The program consumes three values: the pipe's ;; inner radius, its length, and the thickness of its wall. ;; ;; Example: (area-pipe 2 3 4) should produce 251.2 ;; (area-pipe 1 6 4) should produce 301.44 ;; (area-pipe 5 8 2) should produce 678.24 ;; (area-pipe 3 6 1) should produce 285.74 ;; (area-pipe 4 1 4) should produce 226.08 ;; ;; Definition: (define (base-outer-area r t) (- (base-area (+ r t)) (base-area r))) (define (inner-area r l) (* (base-perimeter r) l)) (define (outer-area r l t) (* (base-perimeter (+ r t)) l)) (define (area-pipe r l t) (+ (+ (inner-area r l) (outer-area r l t)) (base-outer-area r t))) ;; ;; Tests: (area-pipe 2 3 4) (area-pipe 1 6 4) (area-pipe 5 8 2) (area-pipe 3 6 1) (area-pipe 4 1 4) ;; Expected values are expressed directly as follows: (= (area-pipe 2 3 4) 251.2) (= (area-pipe 1 6 4) 301.44) (= (area-pipe 5 8 2) 678.24) (= (area-pipe 3 6 1) 285.74) (= (area-pipe 4 1 4) 226.08) ;; ;; ****************************************************************** ;; Extra Problem ;; Contract: convert: num -> num ;; ;; Purpose: It takes one number as input. If the input is even, we ;; square it, and if the number is odd, we return 0. ;; ;; Examples: (convert 1) should produce 0 ;; (convert 2) should produce 4 ;; (convert 7) should produce 0 ;; (convert 4) should produce 16 ;; (convert 11) should produce 0 ;; ;; Unfortunately, it's late and I have absolutely no idea how to do this one.