-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab1.rkt
More file actions
204 lines (153 loc) · 5.14 KB
/
Lab1.rkt
File metadata and controls
204 lines (153 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#lang eopl
;; Do not remove or change the line above!
#|-------------------------------------------------------------------------------
| Name: Connor Haaf
| Pledge: I pledge my honor that I have abided by the stevens honor system.
|-------------------------------------------------------------------------------|#
#| In each lab, you will implement a series of functions.
| Each function you successfully implement will contribute
| some number of points to your total grade.
| The point value of each function is stated above its declaration.
| Each function will be assessed with a set of test cases which
| may NOT be the same as provided test cases.
|#
#|-------------------------------------------------------------------------------|
| Lab 1: Operations (20 PTS) |
|-------------------------------------------------------------------------------|#
#| In this lab, you'll implement some basic operations on integers and booleans.
| The following built-in functions will prove useful:
| (+ x y) returns x + y.
| (* x y) returns x * y.
| (/ x y) returns x / y.
| (sqrt x) returns the square root of x.
| (not p) returns ¬p.
| (and p q) returns p ∧ q.
| (or p q) returns p ∨ q.
| Once you've successfully implemented a function,
| you can use it in subsequent functions too!
| True and false are written in Scheme as "#t" and "#f" respectively.
|#
#|-------------------------------------------------------------------------------|
| Part 1: Arithmetic (8 PTS) |
|-------------------------------------------------------------------------------|#
#| Implement "average" to accept integers x1, x2, and x3,
| and return the average of all three integers: (x1 + x2 + x3) / 3
| Examples:
| (average 1 2 3) -> 2
| (average -10 15 -20) -> -5
| (average -7 32 15) -> 13.333...
| (average 76 145 -349) -> -42.666...
|#
;; Type signature: (average int int int) -> number
;; 2 PTS
(define (average x1 x2 x3)
(/ (+ (+ x1 x2) x3) 3))
#| Implement "hypotenuse" to accept integers a and b,
| and return the hypotenuse c of a triangle with legs a and b.
| c = sqrt(a^2 + b^2).
| Examples:
| (hypotenuse 3 4) -> 5
| (hypotenuse 28 45) -> 53
| (hypotenuse 1 1) -> 1.41421...
| (hypotenuse 6 15) -> 16.15549...
|#
;; Type signature: (quadratic int int) -> number
;; 3 PTS
(define (hypotenuse a b)
(sqrt( +(expt a 2) (expt b 2))))
#| Implement "quadratic" to accept integer coefficients a, b, c,
| and integer variable x, and return a*x^2 + b*x + c.
| Examples:
| (quadratic 0 1 5 -2) -> 3
| (quadratic 1 -1 1 7) -> 43
| (quadratic -5 9 -4 3) -> -22
| (quadratic 35 -223 74 16) -> 5466
|#
;; Type signature: (quadratic int int int int) -> int
;; 3 PTS
(define (quadratic a b c x)
(+(+(* a(* x x))(* b x))c))
#|-------------------------------------------------------------------------------|
| Part 2: Truth Tables (12 PTS) |
|-------------------------------------------------------------------------------|#
#| Implement "nand" so that it accepts two booleans p and q
| and returns p ⊼ q [p nand q]:
|
| p q │ p ⊼ q
| ────┼───────
| T T │ F
| T F │ T
| F T │ T
| F F │ T
|
| Example of testing this function:
| (nand #t #f) -> #t
|#
;; Type signature: (nand boolean boolean) -> boolean
;; 2 PTS
(define (nand p q)
(not(and p q)))
#| Implement "implies" to accept two booleans p and q
| and return "p ⇒ q" [if p, then q]:
|
| p q │ p ⇒ q
| ────┼───────
| T T │ T
| T F │ F
| F T │ T
| F F │ T
|#
;; Type signature: (implies boolean boolean) -> boolean
;; 2 PTS
(define (implies p q)
(not(and(not q) p)))
#| Implement "xor" to accept two booleans p and q
| and return p ⊕ q [p exclusive or q]:
|
| p q │ p ⊕ q
| ────┼───────
| T T │ F
| T F │ T
| F T │ T
| F F │ F
|#
;; Type signature: (xor boolean boolean) -> boolean
;; 2 PTS
(define (xor p q)
(not(or(and p q)(and(not q) (not p)))))
#| Implement "3majority" to return #t iff
| a majority of its three arguments are #t:
|
| p q r │ (3majority p q r)
| ──────┼──────────────────
| T T T │ T
| T F T │ T
| F T T │ T
| F F T │ F
| T T F │ T
| T F F │ F
| F T F │ F
| F F F │ F
|#
;; Type signature: (3majority boolean boolean boolean) -> boolean
;; 3 PTS
(define (3majority p q r)
(or(or(and p q)(and q r))(and p r)))
#| Implement "isosceles" to return #t when
| exactly two of its arguments are #t:
|
| p q r │ (isosceles p q r)
| ──────┼──────────────────
| T T T │ F
| T F T │ T
| F T T │ T
| F F T │ F
| T T F │ T
| T F F │ F
| F T F │ F
| F F F │ F
|#
;; Type signature: (isosceles boolean boolean boolean) -> boolean
;; 3 PTS
(define (isosceles p q r)
(and(or(or(and p q)(and q r))(and p r))(not(and(and(and p q)(and q r))(and p r)))))