-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab_peano.rkt
474 lines (397 loc) · 12.8 KB
/
lab_peano.rkt
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#lang eopl
;;-------------------------------------------------------------------------------
;; Name: Jacob Gerega
;; Pledge: I pledge my honor that I have abided by the Stevens Honor System.
;;-------------------------------------------------------------------------------
;; In this lab, we'll implement Peano arithmetic in Scheme.
;; The Peano axioms use induction to formalize
;; arithmetic with the natural numbers.
;;
;; Peano arithmetic defines the natural numbers inductively.
;; Zero is the initial natural number,
;; One is the successor of zero,
;; Two is the successor of the successor of zero,
;; Three is the successor of the successor of the successor of zero,
;; ... and so on for infinitely many numbers.
;;
;; To represent the natural numbers as these successive "peano numbers",
;; we need a way to programmatically symbolize this inductive (recursive!) process.
;; So we'll use nested lists!
;; We begin by defining zero, our "base case", as the empty list:
(define z '())
;; Then we inductively define the peano numbers:
;; Given a peano number k, the next peano number (k's successor)
;; is one level of nesting deeper than k.
;; Here are the first few peano numbers using our representation:
;; 0 = '()
;; 1 = '(())
;; 2 = '((()))
;; 3 = '(((())))
;; and so on forever...
;;
;; Is this representation cumbersome? Yes, for sure.
;; But we're going to write our functions in such a way
;; that this internal nested list representation of the natural numbers
;; is abstracted away from the arithmetic we implement.
;;-------------------------------------------------------------------------------
;; HELPER FUNCTIONS
;;-------------------------------------------------------------------------------
;; Below are four helper functions given to you to aid in testing your code.
;; You may NOT use any of these functions in the functions you implement!
;; They are strictly for making testing easier.
;; "itp" converts a decimal integer to
;; its corresponding peano representation.
;; The function raises an exception upon receiving a negative number.
;;
;; Examples:
;; (itp 0) -> '()
;; (itp 2) -> '((()))
;; (itp 5) -> '(((((())))))
;; (itp -1) -> <conversion error>
;;
;; Type signature: (itp int) -> peano
(define (itp i)
(cond
[(zero? i) z]
[(positive? i)
(list (itp (- i 1)))]
[else (eopl:error "Error converting to peano: argument cannot be negative!")]))
;; "pti" converts from a nested list peano number
;; to its corresponding decimal number.
;;
;; Examples:
;; (pti '() ) -> 0
;; (pti '((())) ) -> 2
;; (pti '(((((()))))) ) -> 5
;;
;; Type signature: (pti peano) -> int
(define (pti p)
(if (null? p) 0
(+ 1 (pti (car p)))))
;; Here are two functions to expedite running test cases.
;; They do the work for you of converting in and out of
;; our nested list representation of the natural numbers.
;; "tst1" receives a one-input peano function and an integer,
;; converts the integer to peano form, and runs the function on it.
;;
;; Type signature: (tst1 p-function int) -> int/bool
(define (tst1 func a)
(let ([res (func (itp a))])
(if (list? res) (pti res) res)))
;; "tst2" receives a two-input peano function and two integers,
;; converts the integers to peano form, and runs the function on them.
;;
;; Type signature: (tst2 p-function int int) -> int/bool
(define (tst2 func a b)
(let ([res (func (itp a) (itp b))])
(if (list? res) (pti res) res)))
;;-------------------------------------------------------------------------------
;; ATOMIC PEANO OPERATIONS
;;-------------------------------------------------------------------------------
;; Here, we'll write three fundamental operations on peano numbers.
;; From these basic operations, we'll build more complex operations.
;; Implement "pz?", which accepts a peano number p
;; and returns whether p is zero.
;; Recall that in our implementation,
;; we represent zero with the empty list.
;; (Or just use 'equal?' to compare it to the variable 'z')
;;
;; Examples:
;; (tst1 pz? 0) -> #t
;; (tst1 pz? 1) -> #f
;; (tst1 pz? 40) -> #f
;;
;; Type signature: (pz? peano) -> bool
(define (pz? p)
(eq? p '()))
;; Implement "succ", which accepts a peano number p
;; and returns the successor of p.
;; Recall that in our implementation,
;; a peano number's successor is one level of nesting deeper.
;;
;; Examples:
;; (succ '(()) ) -> '((()))
;; (succ '(((()))) ) -> '((((()))))
;; (tst1 succ 0) -> 1
;; (tst1 succ 5) -> 6
;; (tst1 succ 21) -> 22
;; (pti (succ (succ (itp 3)))) -> 5
;;
;; Type signature: (succ peano) -> peano
(define (succ p)
(list p))
;; Implement "pred", which accepts a peano number p
;; and returns the predecessor of p.
;; Recall that in our implementation,
;; each integer is a nested list one level shallower
;; than the next integer.
;;
;; One of Peano's axioms states that zero has no predecessor.
;; So if the input to pred is zero, we need to fail somehow.
;; We'll do this by raising an exception.
;; Put the following line in your code and have it execute
;; when the input value is zero:
;; (eopl:error "pred error: zero has no predecessor!")
;;
;; Examples:
;; (pred '((())) ) -> '(())
;; (pred '() ) -> <pred error>
;; (tst1 pred 1) -> 0
;; (tst1 pred 38) -> 37
;; (pti (pred (pred (pred (itp 14))))) -> 11
;;
;; Type signature: (pred peano) -> peano
(define (pred p)
(if (eq? p '()) ((eopl:error "pred error: zero has no predecessor!")) (car p)))
;;-------------------------------------------------------------------------------
;; COMPLEX PEANO OPERATIONS
;;-------------------------------------------------------------------------------
;; Now that we have the basic operations of
;; succession, predecession, and comparsion with zero,
;; we can implement other familiar arithmetic operations.
;; From this point forward, the fact that we are using nested lists
;; to represent numbers becomes completely irrelevant.
;; The internal use of lists is abstracted away by using
;; the variable 'z', and the functions 'succ', 'pred', and 'pz?'.
;; Pretend you don't know that we're using nested lists:
;; DO NOT use list-related functions like cdr, car, list, null?, and so on.
;; DO NOT use any list literals like '((())).
;; For '(), use z; for '((())), use (succ (succ z)); etc.
;; First, we'll write the addition operator.
;; Implement "p+" to accept two peano numbers
;; and return their sum as a peano number.
;; Again, DO NOT convert in and out of the peano form
;; to do the addition! Use the atomic operations you wrote.
;;
;; Examples:
;; (p+ '((())) '(()) ) -> '(((())))
;; (tst2 p+ 0 0) -> 0
;; (tst2 p+ 5 4) -> 9
;; (tst2 p+ 1 6) -> 7
;; (tst2 p+ 0 10) -> 10
;;
;; Type signature: (p+ peano peano) -> peano
(define (p+ a b)
(if (not (pz? a))
(p+ (pred a) (succ b))
b)
)
;; Next, implement "p-" to accept two peano numbers a and b
;; and return their difference, a - b, as a peano number.
;; This operation has a catch: since there are no negative peano numbers,
;; we can't compute a - b if a < b.
;; If this problem occurs, raise an exception
;; by executing the following line:
;; (eopl:error "subtraction error: a < b!")
;; Since we haven't implemented the '<' operator,
;; figure out how else we'll know
;; when this problem will occur.
;;
;; Examples:
;; (p- '((((())))) '((())) ) -> '((()))
;; (tst2 p- 12 12) -> 0
;; (tst2 p- 6 4) -> 2
;; (tst2 p- 4 6) -> <subtraction error>
;; (tst2 p- 30 19) -> 11
;; (tst2 p- 10 0) -> 10
;;
;; Type signature: (p- peano peano) -> peano
(define (p- a b)
(if (and (pz? a)(not (pz? b)))
(eopl:error "subtraction error: cannot make a negative peano number")
(if (not (pz? b))
(p- (pred a) (pred b))
a))
)
;; Implement "p*" to accept two peano numbers
;; and return their product.
;; If you're not sure how to go about this,
;; think back to how you first learned
;; the concept of multiplication of whole numbers
;; in elementary school -
;; multiplication is just repeated _________.
;; Taking advantage of functions you've already written
;; will make writing this one much easier.
;;
;; Examples:
;; (p* '(((()))) '((())) ) -> '((((((()))))))
;; (tst2 p* 0 0) -> 0
;; (tst2 p* 3 0) -> 0
;; (tst2 p* 4 3) -> 12
;; (tst2 p* 1 7) -> 7
;; (tst2 p* 17 5) -> 85
;;
;; Type signature: (p* peano peano) -> peano
(define (p* a b)
(if (pz? b)
'()
(p+ a (p* a (pred b)))
)
)
;; Implement "p^" to accept peano numbers a and b
;; and return a^b (a raised to the power of b) as a peano number.
;; Like p*, think about how exponentiation is explained
;; in elementary school: it's just repeated _________.
;;
;; Be careful when testing this function.
;; The numbers can get big really quickly,
;; and our implementation with nested lists and recursion
;; won't take too kindly to large computations.
;;
;; You may have 0^0 evaluate to 1 (we aren't going to test it).
;;
;; Examples:
;; (p^ '((())) '(((()))) ) -> '((((((((()))))))))
;; (tst2 p^ 0 4) -> 0
;; (tst2 p^ 5 0) -> 1
;; (tst2 p^ 4 2) -> 16
;; (tst2 p^ 3 3) -> 27
;; (tst2 p^ 2 7) -> 128
;;
;; Type signature: (p^ peano peano) -> peano
(define (p^ a b)
(if (pz? b)
'(())
(p* a (p^ a (pred b)))
)
)
;; Implement "p=" to accept two peano numbers
;; and return whether they are equal.
;; Pretend we don't have a built-in Scheme function
;; to compare the two inputs.
;; The only function we have to work with
;; for comparing peano numbers is "pz?".
;;
;; Examples:
;; (p= '((())) '(()) ) -> #f
;; (p= '(((()))) '(((()))) ) -> #t
;; (tst2 p= 3 16) -> #f
;; (tst2 p= 5 5) -> #t
;; (tst2 p= 0 0) -> #t
;;
;; Type signature: (p= peano peano) -> bool
(define (p= a b)
(if (and (pz? a) (pz? b))
#t
(if (or (pz? a) (pz? b))
#f
(p= (pred a) (pred b))
)
)
)
;; Implement "p>" to accept peano numbers a and b
;; and return whether a > b.
;; Again, this isn't trivial because you can't simply
;; convert the inputs to regular integers and
;; compare them with the built-in > operator.
;; And you can't simply subtract because it'll raise an error!
;;
;; Examples:
;; (p> '((())) '(()) ) -> #t
;; (tst2 p> 4 6) -> #f
;; (tst2 p> 3 3) -> #f
;; (tst2 p> 7 2) -> #t
;;
;; Type signature: (p> peano peano) -> bool
(define (p> a b)
(if (and (pz? a) (not (pz? b)))
#f
(if (and (pz? b) (not (pz? a)))
#t
(if (p= a b)
#f
(p> (pred a) (pred b))
)
)
)
)
;; Implement "p>=" to accept peano numbers a and b
;; and return whether a >= b.
;; This one should be really easy now!
;;
;; Examples:
;; (p>= '((())) '(()) ) -> #t
;; (tst2 p>= 4 6) -> #f
;; (tst2 p>= 3 3) -> #t
;; (tst2 p>= 7 2) -> #t
;;
;; Type signature: (p>= peano peano) -> bool
(define (p>= a b)
(if (and (pz? a) (not (pz? b)))
#f
(if (and (pz? b) (not (pz? a)))
#t
(if (p= a b)
#t
(p>= (pred a) (pred b))
)
)
))
;; Implement "p%" to accept peano numbers a and b
;; and return a mod b (a % b),
;; the remainder of dividing a by b.
;; Remember: integer division is repeated ________.
;; The modulo operator is undefined when
;; the divisor is zero, so once again we'll introduce an error.
;; Execute the following code to raise an exception if b is zero:
;; (eopl:error "modulo error: mod is zero!")
;;
;; Examples:
;; (p% '(((((()))))) '(((()))) ) -> '((()))
;; (tst2 p% 6 0) -> <modulo error>
;; (tst2 p% 3 5) -> 3
;; (tst2 p% 0 6) -> 0
;; (tst2 p% 30 15) -> 0
;; (tst2 p% 8 1) -> 0
;; (tst2 p% 124 17) -> 5
;; (tst2 p% 76 9) -> 4
;;
;; Type signature: (p% peano peano) -> peano
(define (p% a b)
(if (pz? b)
(eopl:error "modulus error: division by zero")
(if (p>= a b)
(p% (p- a b) b)
a
)
)
)
;; Implement "p-even?" to accept a peano number p
;; and return whether p is even.
;;
;; Examples:
;; (p-even? '((((())))) ) -> #t
;; (tst1 p-even? 0) -> #t
;; (tst1 p-even? 1) -> #f
;; (tst1 p-even? 25) -> #f
;; (tst1 p-even? 76) -> #t
;;
;; Type signature: (p-even? peano) -> bool
(define (p-even? p)
(eq? (p% p '((()))) '()))
;; Implement "p-prime?" to accept a peano number p
;; and return whether p is a prime number.
;; Zero and one are not prime.
;; You'll likely need to write a helper function for this one!
;;
;; Examples:
;; (p-prime? '(((((((()))))))) ) -> #t
;; (tst1 p-prime? 0) -> #f
;; (tst1 p-prime? 2) -> #t
;; (tst1 p-prime? 5) -> #t
;; (tst1 p-prime? 9) -> #f
;; (tst1 p-prime? 35) -> #f
;; (tst1 p-prime? 37) -> #t
;;
;; Type signature: (p-prime? peano) -> bool
(define (p-prime? p)
(p-primehelper p '((()))))
(define (p-primehelper p n)
(if (p= p n)
#t
(if (eq? (p% p n) '())
#f
(p-primehelper p (succ n)))
)
)
;; Created March 2020 by Jared Pincus