1 12687dd9 2023-08-04 jrmu ;; The first three lines of this file were inserted by DrScheme. They record metadata
2 12687dd9 2023-08-04 jrmu ;; about the language level of this file in a form that our tools can easily process.
3 12687dd9 2023-08-04 jrmu #reader(lib "htdp-beginner-reader.ss" "lang")((modname 10.1.1) (read-case-sensitive #t) (teachpacks ((lib "convert.ss" "teachpack" "htdp") (lib "guess.ss" "teachpack" "htdp") (lib "master.ss" "teachpack" "htdp") (lib "draw.ss" "teachpack" "htdp") (lib "hangman.ss" "teachpack" "htdp") (lib "sound.ss" "installed-teachpacks"))) (htdp-settings #8(#t constructor repeating-decimal #f #t none #f ((lib "convert.ss" "teachpack" "htdp") (lib "guess.ss" "teachpack" "htdp") (lib "master.ss" "teachpack" "htdp") (lib "draw.ss" "teachpack" "htdp") (lib "hangman.ss" "teachpack" "htdp") (lib "sound.ss" "installed-teachpacks")))))
4 12687dd9 2023-08-04 jrmu (define PAYPERHOUR 14)
6 12687dd9 2023-08-04 jrmu ;Data Definition
7 12687dd9 2023-08-04 jrmu ;A list-of-numbers is either
8 12687dd9 2023-08-04 jrmu ;1. an empty list or
9 12687dd9 2023-08-04 jrmu ;2. (cons n lon) where n is a number and lon is a list-of-numbers.
11 12687dd9 2023-08-04 jrmu ;Contract, Purpose, Header
12 12687dd9 2023-08-04 jrmu ;wage : number -> number
13 12687dd9 2023-08-04 jrmu ;Computes the wage given hours.
15 12687dd9 2023-08-04 jrmu (define (wage hours)
16 12687dd9 2023-08-04 jrmu (* PAYPERHOUR hours))
18 12687dd9 2023-08-04 jrmu ;Contract, Purpose, Header
19 12687dd9 2023-08-04 jrmu ;hours->wages : list-of-numbers -> list-of-numbers
20 12687dd9 2023-08-04 jrmu ;Computes the wages (list-of-numbers) from alon.
24 12687dd9 2023-08-04 jrmu (define (hours->wages alon)
26 12687dd9 2023-08-04 jrmu [(empty? alon) empty]
27 12687dd9 2023-08-04 jrmu [(> (first alon) 100) (error 'hours->wages "too many hours")]
28 12687dd9 2023-08-04 jrmu [else (cons (wage (first alon)) (hours->wages (rest alon)))]))
30 12687dd9 2023-08-04 jrmu (define list1 (cons 5 (cons 8 (cons 12 (cons 9 (cons 200 empty))))))
32 12687dd9 2023-08-04 jrmu ;Contract, Purpose, Header
33 12687dd9 2023-08-04 jrmu ;fahrenheit->celsius : number -> number
35 12687dd9 2023-08-04 jrmu (define (fahrenheit->celsius fahr)
36 12687dd9 2023-08-04 jrmu (* 5/9 (- fahr 32)))
38 12687dd9 2023-08-04 jrmu ;Contract, Purpose, Header
39 12687dd9 2023-08-04 jrmu ;convertFC : list-of-numbers -> list-of-numbers
40 12687dd9 2023-08-04 jrmu ;Converts alon (in Fahrenheit) to Celsius.
42 12687dd9 2023-08-04 jrmu (define (convertFC alon)
44 12687dd9 2023-08-04 jrmu [(empty? alon) empty]
45 12687dd9 2023-08-04 jrmu [else (cons (fahrenheit->celsius (first alon)) (convertFC (rest alon)))]))
47 12687dd9 2023-08-04 jrmu ;Contract, Purpose, Header
48 12687dd9 2023-08-04 jrmu ;usd->euro : number -> number
49 12687dd9 2023-08-04 jrmu ;Converts usd to euros.
51 12687dd9 2023-08-04 jrmu (define (usd->euro usd)
52 12687dd9 2023-08-04 jrmu (* usd EXCHRATE))
54 12687dd9 2023-08-04 jrmu (define EXCHRATE 1.22)
56 12687dd9 2023-08-04 jrmu ;Contract, Purpose, Header
57 12687dd9 2023-08-04 jrmu ;convert-euro-1 : list-of-numbers -> list-of-numbers
58 12687dd9 2023-08-04 jrmu ;Converts alon (a list-of-numbers in US dollars) to euros.
60 12687dd9 2023-08-04 jrmu (define (convert-euro-1 alon)
62 12687dd9 2023-08-04 jrmu [(empty? alon) empty]
63 12687dd9 2023-08-04 jrmu [else (cons (usd->euro (first alon)) (convert-euro-1 (rest alon)))]))
65 12687dd9 2023-08-04 jrmu ;Contract, Purpose, Header
66 12687dd9 2023-08-04 jrmu ;eliminate-exp : list-of-numbers number -> list-of-numbers
67 12687dd9 2023-08-04 jrmu ;Eliminates all toys within lotp that exceed the price of ua and returns
68 12687dd9 2023-08-04 jrmu ;the remaining list.
72 12687dd9 2023-08-04 jrmu ;(define (eliminate-exp lotp ua)
74 12687dd9 2023-08-04 jrmu ; [(empty? lotp) empty]
75 12687dd9 2023-08-04 jrmu ; [(> (first lotp) ua) ... (first lotp) (eliminate-exp (rest lotp)) ...]
76 12687dd9 2023-08-04 jrmu ; [else (first lotp) (eliminate-exp (rest lotp))...]))
78 12687dd9 2023-08-04 jrmu (define (eliminate-exp lotp ua)
80 12687dd9 2023-08-04 jrmu [(empty? lotp) empty]
81 12687dd9 2023-08-04 jrmu [(> (first lotp) ua) (eliminate-exp (rest lotp) ua)]
82 12687dd9 2023-08-04 jrmu [(<= (first lotp) ua) (cons (first lotp) (eliminate-exp (rest lotp) ua))]))
84 12687dd9 2023-08-04 jrmu (define list2 (cons 4.5 (cons 4.6 (cons 4.7 (cons 4.9 (cons 5.2 (cons 3.8 (cons 2.9 (cons 3.4 (cons 6.1 empty))))))))))
86 12687dd9 2023-08-04 jrmu ;Data Definition
88 12687dd9 2023-08-04 jrmu ;A list-of-symbols is either
89 12687dd9 2023-08-04 jrmu ;1. an empty list or
90 12687dd9 2023-08-04 jrmu ;2. (cons s los) where s is a symbol and los is a list-of-symbols.
92 12687dd9 2023-08-04 jrmu ;Contract, Purpose, Header
93 12687dd9 2023-08-04 jrmu ;substitute : symbol symbol list-of-symbols -> list-of-symbols
94 12687dd9 2023-08-04 jrmu ;Replace old with new (numbers) in alos (list-of-symbols).
97 12687dd9 2023-08-04 jrmu (define (substitute new old alos)
99 12687dd9 2023-08-04 jrmu [(empty? alos) empty]
100 12687dd9 2023-08-04 jrmu [(symbol=? (first alos) old) (cons new (substitute new old (rest alos)))]
101 12687dd9 2023-08-04 jrmu [else (cons (first alos) (substitute new old (rest alos)))]))
103 12687dd9 2023-08-04 jrmu ;Contract, Purpose, Header
104 12687dd9 2023-08-04 jrmu ;recall : symbol list-of-symbols -> list-of-symbols
105 12687dd9 2023-08-04 jrmu ;Removes ty from lon (list of names, data type: list-of-symbols).
107 12687dd9 2023-08-04 jrmu (define (recall ty lon)
109 12687dd9 2023-08-04 jrmu [(empty? lon) empty]
110 12687dd9 2023-08-04 jrmu [(symbol=? (first lon) ty) (recall ty (rest lon)) ]
111 12687dd9 2023-08-04 jrmu [else (cons (first lon) (recall ty (rest lon)))]))
113 12687dd9 2023-08-04 jrmu (define list3 (cons 'firetruck (cons 'playstation (cons 'xbox (cons 'racecar (cons 'doll (cons 'bicycle empty)))))))
115 12687dd9 2023-08-04 jrmu ;; how-many : number number number -> number
116 12687dd9 2023-08-04 jrmu ;; Determines how many solutions a given quadratic equation has given
117 12687dd9 2023-08-04 jrmu ;; coefficients a, b, and c.
119 12687dd9 2023-08-04 jrmu (define (how-many a b c)
121 12687dd9 2023-08-04 jrmu [(= a 0) 'degenerate]
122 12687dd9 2023-08-04 jrmu [(> (sqr b) (* 4 a c)) 2]
123 12687dd9 2023-08-04 jrmu [(= (sqr b) (* 4 a c)) 1]
124 12687dd9 2023-08-04 jrmu [(< (sqr b) (* 4 a c)) 0]))
126 12687dd9 2023-08-04 jrmu ;Contract, Purpose, Header
127 12687dd9 2023-08-04 jrmu ;quadratic-roots : number number number -> Scheme-value
128 12687dd9 2023-08-04 jrmu ;Given a, b, and c (numbers), it returns either a symbol ('none), a number (if there is one root to the eqn), or a list of 2 numbers by calling on two-roots (if there are two solutions).
130 12687dd9 2023-08-04 jrmu (define (quadratic-roots a b c)
132 12687dd9 2023-08-04 jrmu [(= (how-many a b c) 0) 'none]
133 12687dd9 2023-08-04 jrmu [(= (how-many a b c) 1) (/ b -2 a)]
134 12687dd9 2023-08-04 jrmu [(= (how-many a b c) 2) (two-roots a b c)]))
136 12687dd9 2023-08-04 jrmu ;two-roots : number number number -> list-of-numbers
137 12687dd9 2023-08-04 jrmu ;Given a, b, and c, computes the two roots of the
138 12687dd9 2023-08-04 jrmu ;quadratic equation and returns the 2 roots
139 12687dd9 2023-08-04 jrmu ;as a list-of-numbers.
141 12687dd9 2023-08-04 jrmu (define (two-roots a b c)
143 12687dd9 2023-08-04 jrmu (+ (* -1 b)
144 12687dd9 2023-08-04 jrmu (sqrt (- (sqr b)
145 12687dd9 2023-08-04 jrmu (* 4 a c))))
148 12687dd9 2023-08-04 jrmu (- (* -1 b)
149 12687dd9 2023-08-04 jrmu (sqrt (- (sqr b)
150 12687dd9 2023-08-04 jrmu (* 4 a c))))
151 12687dd9 2023-08-04 jrmu 2 a) empty)))
153 12687dd9 2023-08-04 jrmu ;Data Definition
154 12687dd9 2023-08-04 jrmu ;A mixed-list is either
155 12687dd9 2023-08-04 jrmu ;1. an empty list or
156 12687dd9 2023-08-04 jrmu ;2. (cons mixed list) where mixed is either a symbol or a number
157 12687dd9 2023-08-04 jrmu ; and list is a mixed-list.
159 12687dd9 2023-08-04 jrmu ;Contract, Purpose, Header
160 12687dd9 2023-08-04 jrmu ;controller : number -> mixed-list
161 12687dd9 2023-08-04 jrmu ;Controller takes in cents and returns a list with the following elements:
162 12687dd9 2023-08-04 jrmu ;1. the dollar amount (number),
163 12687dd9 2023-08-04 jrmu ;2. 'dollar or 'dollars,
165 12687dd9 2023-08-04 jrmu ;4. the cent amount (number),
166 12687dd9 2023-08-04 jrmu ;5. 'cent or 'cents.
168 12687dd9 2023-08-04 jrmu ;Whether singular or plural is used for elements 2 and 5 is
169 12687dd9 2023-08-04 jrmu ;determined using plural?.
172 12687dd9 2023-08-04 jrmu (define (controller cents)
173 12687dd9 2023-08-04 jrmu (cons (quotient cents 100)
174 12687dd9 2023-08-04 jrmu (cons (cond
175 12687dd9 2023-08-04 jrmu [(plural? (quotient cents 100)) 'dollars]
176 12687dd9 2023-08-04 jrmu [else 'dollar])
178 12687dd9 2023-08-04 jrmu (cons (remainder cents 100)
179 12687dd9 2023-08-04 jrmu (cons (cond
180 12687dd9 2023-08-04 jrmu [(plural? (remainder cents 100)) 'cents]
181 12687dd9 2023-08-04 jrmu [else 'cent]) empty))))))
183 12687dd9 2023-08-04 jrmu ;Contract, Purpose, Header
184 12687dd9 2023-08-04 jrmu ;plural? : number -> boolean
185 12687dd9 2023-08-04 jrmu ;Determines if a number needs to be considered plural
186 12687dd9 2023-08-04 jrmu ;for grammar purposes.
188 12687dd9 2023-08-04 jrmu (define (plural? num)
190 12687dd9 2023-08-04 jrmu [(= num 1) false]
191 12687dd9 2023-08-04 jrmu [(not (= num 1)) true]))