Session #11
Answers
Define a data definition named LetterGrade that represents exactly the three strings "A", "B", and "C". Include the type comment, an interpretation, and the template for a function that consumes a LetterGrade.
; LetterGrade is one of
; - "A"
; - "B"
; - "C"
; LetterGrade represents a letter grade as one of A,B or C
(define (fn-for-lettergrade lg)
(cond [(string=? "A") (... lg)]
[(string=? "B") (... lg)]
[(string=? "C") (... lg)]))
; LetterGrade is one of
; - "A"
; - "B"
; - "C"
; LetterGrade represents a letter grade as one of A,B or C
(define (fn-for-lettergrade lg)
(cond [(string=? "A") (... lg)]
[(string=? "B") (... lg)]
[(string=? "C") (... lg)]))
; LetterGrade is one of:
; - "A"
; - "B"
; - "C"
; interp. a letter grade earned by a student
#;
(define (fn-for-letter-grade lg)
(cond [(string=? lg "A") (...)]
[(string=? lg "B") (...)]
[(string=? lg "C") (...)]))
; LetterGrade is one of:
; - "A"
; - "B"
; - "C"
; interp. a letter grade earned by a student
#;
(define (fn-for-letter-grade lg)
(cond [(string=? lg "A") (...)]
[(string=? lg "B") (...)]
[(string=? lg "C") (...)]))
Define a data definition named SeatNum for legal seat numbers from 1 through 32 inclusive. Include the type comment, an interpretation, and the basic template for a function that consumes a SeatNum.
; SeatNum is [1,32]Integer
; represents the value of a given seats position
(define (fn-for-s s) (... s))
```racket ; SeatNum is [1,32]Integer ; represents the value of a given seats position (define (fn-for-s s) (... s))
; SeatNum is Integer[1,32]
; interp. the number of a legal seat
#;
(define (fn-for-seat-num sn)
(... sn))
; SeatNum is Integer[1,32] ; interp. the number of a legal seat #; (define (fn-for-seat-num sn) (... sn))
Consider this itemization: Status is one of "closed" or an Integer from 1 through 10 inclusive. Write a template for a function that consumes a Status. Make sure the numeric test cannot accidentally be applied to the string case.
; Status is one of
; - "closed"
; - [1, 10]
; Status represents the a status of closed or a integer representing how much longer until we are closed
(define (STATUS1 "closed"))
(define (STATUS2 1))
(define (STATUS3 10))
(define (fn-for-status s)
(cond [(and (number? s) (>= s 0) (<= s 10)) (... s)]
[(string=? "closed") (... s)]))
```racket
; Status is one of
; - "closed"
; - [1, 10]
; Status represents the a status of closed or a integer representing how much longer until we are closed
(define (STATUS1 "closed"))
(define (STATUS2 1))
(define (STATUS3 10))
(define (fn-for-status s)
(cond [(and (number? s) (>= s 0) (<= s 10)) (... s)]
[(string=? "closed") (... s)]))
#;
(define (fn-for-status s)
(cond [(and (string? s) (string=? s "closed")) (...)]
[(and (integer? s) (<= 1 s) (<= s 10)) (... s)]))
A simpler first branch such as (string=? s "closed") is also acceptable if it is guaranteed to be reached only for strings.
#;
(define (fn-for-status s)
(cond [(and (string? s) (string=? s "closed")) (...)]
[(and (integer? s) (<= 1 s) (<= s 10)) (... s)]))
A simpler first branch such as (string=? s "closed") is also acceptable if it is guaranteed to be reached only for strings.
In your own words, explain the difference between a function purpose statement and the interpretation in a data definition.
function purpose defines what the function should do. This mean it states the relationship between the input values and the output values.
The data interpretation defines how the information of the problem domain is represented as data in the program
function purpose defines what the function should do. This mean it states the relationship between the input values and the output values. The data interpretation defines how the information of the problem domain is represented as data in the program
A function purpose describes the relationship between the data a particular function consumes and the result it produces. A data interpretation explains what values of the data definition mean as information in the problem domain.
A function purpose describes the relationship between the data a particular function consumes and the result it produces. A data interpretation explains what values of the data definition mean as information in the problem domain.
Suppose an itemization says a value is either Integer[0,10] or Integer[5,20]. What problem does the value 7 create? If a cond checks the first interval before the second, what will happen?
- it's difficult to tell which one 7 should be.
- Cond will always evaluate true to the first interval for overlapping portions
1. it's difficult to tell which one 7 should be. 2. Cond will always evaluate true to the first interval for overlapping portions
The subclasses overlap, so 7 belongs to both and the data definition does not uniquely identify which subclass it represents. A cond uses the first true clause, so if the 0-to-10 interval is checked first, 7 will always be handled by that first branch. The itemization should normally be redesigned so its cases are unambiguous, or the intended priority must be made explicit.
The subclasses overlap, so 7 belongs to both and the data definition does not uniquely identify which subclass it represents. A cond uses the first true clause, so if the 0-to-10 interval is checked first, 7 will always be handled by that first branch. The itemization should normally be redesigned so its cases are unambiguous, or the intended priority must be made explicit.
A parking garage has levels numbered 1 through 8 inclusive. Define a data definition named LevelNum with a precise type comment, an interpretation, and the basic template for a function that consumes a LevelNum.
; LevelNum is Integer[1, 8]
; LevelNum represents the current floor of a parking garage
(define (fn-for-level-num ln) (... ln))
; LevelNum is Integer[1, 8] ; LevelNum represents the current floor of a parking garage (define (fn-for-level-num ln) (... ln))
; LevelNum is Integer[1,8]
; interp. a legal parking-garage level from 1 through 8
#;
(define (fn-for-level-num n)
(... n))
; LevelNum is Integer[1,8] ; interp. a legal parking-garage level from 1 through 8 #; (define (fn-for-level-num n) (... n))
TrafficLight is one of the strings "red", "yellow", or "green". Write the data definition and template. In the template, use placeholders for unfinished function behavior rather than accidentally calling the parameter as a function.
; TrafficLight is one of
; - "red"
; - "yellow"
; - "green"
; TraffcLight represents the color of a currently on traffic light
(define (fn-for-tl tl)
(cond [(string=? "red") (... tl)]
[(string=? "yellow") (... tl)]
[(string=? "green") (... tl)]))
```racket
; TrafficLight is one of
; - "red"
; - "yellow"
; - "green"
; TraffcLight represents the color of a currently on traffic light
(define (fn-for-tl tl)
(cond [(string=? "red") (... tl)]
[(string=? "yellow") (... tl)]
[(string=? "green") (... tl)]))
; TrafficLight is one of:
; - "red"
; - "yellow"
; - "green"
; interp. the current state of a traffic light
#;
(define (fn-for-traffic-light tl)
(cond [(string=? tl "red") (...)]
[(string=? tl "yellow") (...)]
[(string=? tl "green") (...)]))
; TrafficLight is one of:
; - "red"
; - "yellow"
; - "green"
; interp. the current state of a traffic light
#;
(define (fn-for-traffic-light tl)
(cond [(string=? tl "red") (...)]
[(string=? tl "yellow") (...)]
[(string=? tl "green") (...)]))
A data definition says TemperatureRange is either Integer[-10,10] or Integer[5,30]. Explain why this is a problematic itemization. Then explain what happens to the value 7 if a cond checks the -10-to-10 case before the 5-to-30 case.
This is problematic because you can't tell which one the 7 is suppose to belong to. When you do this with a cond the first one that matches will always trigger. The overlapping portions can never be attributed the first subclass
This is problematic because you can't tell which one the 7 is suppose to belong to. When you do this with a cond the first one that matches will always trigger. The overlapping portions can never be attributed the first subclass
The two subclasses overlap because values from 5 through 10 belong to both intervals. Therefore the data definition does not uniquely determine which subclass values such as 7 belong to. cond chooses the first true clause, so if Integer[-10,10] is checked first, 7 will always be handled by that branch. The data definition should normally be redesigned so the subclasses are unambiguous.
The two subclasses overlap because values from 5 through 10 belong to both intervals. Therefore the data definition does not uniquely determine which subclass values such as 7 belong to. cond chooses the first true clause, so if Integer[-10,10] is checked first, 7 will always be handled by that branch. The data definition should normally be redesigned so the subclasses are unambiguous.