-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab_truth_table.rkt
127 lines (92 loc) · 2.49 KB
/
lab_truth_table.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
#lang eopl
;;-------------------------------------------------------------------------------
;; Name: Jacob Gerega
;; Pledge: I pledge my honor that I have abided by the Stevens Honor System.
;;-------------------------------------------------------------------------------
;; In this lab, you'll implement some basic logic operators using
;; Scheme's built-in "and", "or", and "not" functions.
;; Keep in mind that you can also define functions in terms of
;; other functions you've already implemented in this file!
;;
;; Remember that Scheme uses prefix notation,
;; so "p and q" is written "(and p q)".
;;
;; True and false are written in Scheme as "#t" and "#f".
;; Implement the function (nand p q) to return "p nand q":
;;
;; p q | p nand q
;; ---------------
;; T T | F
;; T F | T
;; F T | T
;; F F | T
;; Type signature: (nand boolean boolean) -> boolean
(define (nand p q)
(not (and p q)))
;; Implement (implies p q) to return "p implies q" (if p, then q):
;;
;; p q | p implies q
;; ------------------
;; T T | T
;; T F | F
;; F T | T
;; F F | T
;; Type signature: (implies boolean boolean) -> boolean
(define (implies p q)
(or (not p) q))
;; Implement (xor p q) to return "p xor q" (exclusive or):
;;
;; p q | p xor q
;; --------------
;; T T | F
;; T F | T
;; F T | T
;; F F | F
;; Type signature: (xor boolean boolean) -> boolean
(define (xor p q)
(and (or p q) (not (and p q))))
;; Implement (nor p q) to return "p nor q":
;;
;; p q | p nor q
;; --------------
;; T T | F
;; T F | F
;; F T | F
;; F F | T
;; Type signature: (nor boolean boolean) -> boolean
(define (nor p q)
(and (not p) (not q)))
;; Implement (3majority p q r) to return #t when
;; 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
(define (3majority p q r)
(or (and p q) (or (and q r) (and p r))))
;; Implement (isosceles p q r) 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
(define (isosceles p q r)
(or (and p q) (or (and q r) (and p r))))
;; Created January 2018 by Samuel Kraus and Edward Minnix
;; Updated January 2020 by Jared Pincus