-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRacketLab3
143 lines (114 loc) · 3.95 KB
/
RacketLab3
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
; Lab 3
(define WPI_ID "ajli") ; please put in your username, the text before the @ in your email address
; ; Problem 1
; ; Given the following data definition for Food and SpecialLunch
; ; Write a function that gets the calories of the drink in the lunch
; ; pay special attention to the interpretation of the SpecialLunch
(define-struct food (name calories healthy?))
; a Food is a (make-food String Natural Boolean)
; interp.
; - name is the name of the food
; - calories is the number of calories in 1 serving of this food
; - true if the food is considered healthy
; Examples(Defining Variables):
(define PIZZA (make-food "Pizza" 350 #false))
(define CHIPS (make-food "Chips" 210 #false))
(define SPORTS-DRINK (make-food "Electromight" 90 #true))
(define PUDDING (make-food "Pudding" 110 #false))
; Template:
#;
(define (food-fcn a-food)
(...
(food-name a-food)
(food-calories a-food)
(food-healthy? a-food)))
(define-struct splunch (entree side drink dessert))
; a SpecialLunch is a (make-splunch Food Food Food Food)
; interp.
; - entree is the primary food item and has 2 servings
; - side is a secondary food item and has 1 serving
; - drink is a liquid food item and has 3 servings if it is healhy and 1 if it is not
; - dessert is a sweet food item and we don't count/consider its calories if it is healthy
; Example:
(define GRAB-N-GO (make-splunch PIZZA CHIPS SPORTS-DRINK PUDDING))
; Template:
#;
(define (splunch-fcn a-splunch)
(...
(splunch-entree a-splunch)
(splunch-side a-splunch)
(splunch-drink a-splunch)
(splunch-dessert a-splunch)))
; Step 1:
; drink-calories : SpecialLunch -> Natural
; consumes a special lunch
; produces the number of calories in the drink of the lunch using the logic in the SpecialLunch interp.
; stub:
;(define (drink-calories a-splunch) 0)
; Step 2:
(check-expect (drink-calories GRAB-N-GO) 270)
(check-expect (drink-calories (make-splunch
PIZZA
CHIPS
(make-food "Soda" 200 #false)
PUDDING))
200)
; STEP 3:
; (define (drink-calories a-splunch)
; (... (food-healthy? (splunch-drink a-splunch))
; (food-calories (splunch-drink a-splunch)) ))
; Step 4:
(define (drink-calories a-splunch)
(if (food-healthy? (splunch-drink a-splunch))
(* 3 (food-calories (splunch-drink a-splunch)))
(food-calories (splunch-drink a-splunch))))
; Step 5 Test and Debug (More Tests):
(check-expect (drink-calories GRAB-N-GO) 270)
(check-expect (drink-calories (make-splunch
PIZZA
CHIPS
(make-food "Soda" 200 #false)
PUDDING))
200)
(check-expect (drink-calories (make-splunch
PIZZA
CHIPS
(make-food "Water" 0 #true)
PUDDING))
0)
(check-expect (drink-calories (make-splunch
PIZZA
CHIPS
(make-food "Juice" 120 #true)
PUDDING))
360)
(check-expect (drink-calories (make-splunch
PIZZA
CHIPS
(make-food "Milkshake" 250 #false)
PUDDING))
250)
; ; Problem 2
; ; Implement the following function `strings-lengths`
; ; Using the ListOfString and ListOfNumber definitions from lecture
; Step 1: strings-lengths: ListOfString -> ListOfNumber
; consumes a list of strings
; produces a list of the length of each string in the same order
;(define (strings-lengths a-los) empty)
; Examples Step 2:
(check-expect (strings-lengths (cons "a" (cons "bb" (cons "ccc" (cons "dddd" empty)))))
(cons 1 (cons 2 (cons 3 (cons 4 empty)))))
;Step 3:
; (define (strings-lengths a-los)
; (cond [(empty? a-los) empty]
; [else (cons (... (first a-los)) (strings-lengths (... a-los)))]))
;Step 4:
(define (strings-lengths a-los)
(cond [(empty? a-los) empty]
[else (cons (string-length (first a-los)) (strings-lengths (rest a-los)))]))
;Step 5
(check-expect (strings-lengths (cons "a" (cons "bb" (cons "ccc" (cons "dddd" empty)))))
(cons 1 (cons 2 (cons 3 (cons 4 empty)))))
(check-expect (strings-lengths (cons "hello" (cons "world" (cons "!" empty))))
(cons 5 (cons 5 (cons 1 empty))))
(check-expect (strings-lengths empty) empty)