Session #10
Answers
What is the main purpose of a data definition?
Which chain best matches the design principle in the notes?
When is an enumeration appropriate?
Which type comment describes an inclusive integer interval from 1 through 32?
What is the template shape for an atomic non-distinct type such as CityName is String?
What makes an itemization different from a simple enumeration?
How does cond choose an answer?
Why might an itemization cond use (and (number? c) (<= 1 c) ...)?
Why can the last numeric subclass sometimes be tested with only (number? c)?
What does it mean that HtDF and HtDD are orthogonal?
If LetterGrade has exactly "A", "B", and "C", how many main cond branches should its template have?
What is the interpretation in a data definition for?
A function consumes SeatNum defined as Integer[1,32]. Is 50 a legal input according to the data definition?
Which statement correctly distinguishes purpose from interpretation?
Why should tests for an itemization cover every subclass?
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 an enumeration
; - "A"
; - "B"
; - "C"
; Represents the letter grade as either A, B or C
(define (fn-for-letter-grade l)
(cond [(string=? "A" l) (l)]
[(string=? "B" l) (l)]
[else l]))
```racket
; LetterGrade is an enumeration
; - "A"
; - "B"
; - "C"
; Represents the letter grade as either A, B or C
(define (fn-for-letter-grade l)
(cond [(string=? "A" l) (l)]
[(string=? "B" l) (l)]
[else l]))
; 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 Number
; SeatNum represents the number of a seat from 1 to 32 inclusive
(define (fn-on-seat-num n) n)
```racket ; SeatNum is Number ; SeatNum represents the number of a seat from 1 to 32 inclusive (define (fn-on-seat-num n) n) ```
; 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 Enumeration
; - "closed"
; - [1, 10]
; Status represents the value until closed or the closed value itself
(define (fn-on-status s)
(cond [(number? s) (s)]
[else s]))
```racket
; Status is Enumeration
; - "closed"
; - [1, 10]
; Status represents the value until closed or the closed value itself
(define (fn-on-status s)
(cond [(number? s) (s)]
[else 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.
the function purpose is what the function is suppose to do or the relationship between the input data and the output data. Interpretation of the data is how that data is suppose to represent the information about the problem domain
the function purpose is what the function is suppose to do or the relationship between the input data and the output data. Interpretation of the data is how that data is suppose to represent the information about 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.
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?
You can tell which interval the 7 belongs to so you need to redo the data definition
You can tell which interval the 7 belongs to so you need to redo the data definition
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.