-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuiz 2 - Simple Data
121 lines (92 loc) · 2.72 KB
/
Quiz 2 - Simple Data
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
;; HtDD Design Quiz
;; Age is Natural
;; interp. the age of a person in years
(define A0 18)
(define A1 25)
#;
(define (fn-for-age a)
(... a))
;; Template rules used:
;; - atomic non-distinct: Natural
; Problem 1:
;
; Consider the above data definition for the age of a person.
;
; Design a function called teenager? that determines whether a person
; of a particular age is a teenager (i.e., between the ages of 13 and 19).
; Age -> Boolean
; produces true if age between 13,19
(check-expect (teenager? 3) false)
(check-expect (teenager? 13) false)
(check-expect (teenager? 15) true)
(check-expect (teenager? 19) false)
(check-expect (teenager? 20) false)
;(define (teenager? a) true) ; stub
;<use template from data def.>
(define (teenager? a)
(and (> a 13) (< a 19)))
; Problem 2:
;
; Design a data definition called MonthAge to represent a person's age
; in months.
;; MonthAge is Natural
;; interp. the age of person in months
(define MA1 30)
(define MA2 3)
#;(define (month-age-fn-name c)
(... c))
;; Templates rules used :
;; - atomic non-distonct : Natural
; Problem 3:
;
; Design a function called months-old that takes a person's age in years
; and yields that person's age in months.
;
;; Age -> MonthAge
;; takes a person's age in years and yields that person's age in months.
(check-expect (months-old 3) (* 3 12))
(check-expect (months-old 5) (* 5 12))
;; (define (months-old c) 0) ;stub
;;<use data def. template>
(define (months-old c)
(* c 12))
; Problem 4:
;
; Consider a video game where you need to represent the health of your
; character. The only thing that matters about their health is:
;
; - if they are dead (which is shockingly poor health)
; - if they are alive then they can have 0 or more extra lives
;
; Design a data definition called Health to represent the health of your
; character.
;
; Design a function called increase-health that allows you to increase the
; lives of a character. The function should only increase the lives
; of the character if the character is not dead, otherwise the character
; remains dead.
;; ch-health is one of:
;; false
;; Natural
;; false means the character is dead & Natural points to how many lives he have
(define CH1 false)
(define CH2 10)
#;
(define (health-fn-name c)
(cond
[(false? c) (...)]
[else (... c)]))
;; Rules used in template:
;;- one of 2 cases
;; - atomic non-distinct : Natural
;; - atomic distinct : false
;; ch-health -> ch-health
;; increases the health of character if NOT dead (remains dead if dead)
(check-expect (increase-health 10) 11)
(check-expect (increase-health false) false)
;;(define (increase-health c) 0) ;stub
;<uses def. template>
(define (increase-health c)
(cond
[(false? c) false]
[else (+ c 1)]))