Blob


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