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 ())))
7 ;A list-of-numbers is either
9 ;2. (cons n lon) where n is a number and lon is a list-of-numbers.
11 ;Contract, Purpose, Header
12 ;wage : number -> number
13 ;Computes the wage given hours.
18 ;Contract, Purpose, Header
19 ;hours->wages : list-of-numbers -> list-of-numbers
20 ;Computes the wages (list-of-numbers) from alon.
24 (define (hours->wages alon)
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)
38 ;Contract, Purpose, Header
39 ;convertFC : list-of-numbers -> list-of-numbers
40 ;Converts alon (in Fahrenheit) to Celsius.
42 (define (convertFC alon)
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)
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)
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
72 ;(define (eliminate-exp lotp ua)
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)
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))))))))))
88 ;A list-of-symbols is either
90 ;2. (cons s los) where s is a symbol and los is a list-of-symbols.
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).
97 (define (substitute new old alos)
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)
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)
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)
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)
154 ;A mixed-list is either
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,
165 ;4. the cent amount (number),
168 ;Whether singular or plural is used for elements 2 and 5 is
169 ;determined using plural?.
172 (define (controller cents)
173 (cons (quotient cents 100)
175 [(plural? (quotient cents 100)) 'dollars]
178 (cons (remainder cents 100)
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)
191 [(not (= num 1)) true]))