7 "Difference between Dates" case study | (4 activities) |
1. Which of the following calls of the first version of the day-span procedure produces an error message? (Check all that apply.) |
2.
Given below are two versions of a procedure to determine whether one height measurement is greater (taller) than another. A height measurement is a two-word sentence that contains some number of feet and some number of inches; the feet and inches are accessed by the feet and inches procedures respectively.
Version 1: (define (is-taller? height1 height2) (cond ((> (feet height1) (feet height2)) #t) ((< (feet height1) (feet height2)) #f) ((> (inches height1) (inches height2)) #t) (else #f) ) ) Version 2: (define (is-taller? height1 height2) (> (+ (* 12 (feet height1)) (inches height1)) (+ (* 12 (feet height2)) (inches height2)) ) )Which of the two versions above is more similar to the first version of the day-span procedure, and which is more similar to the second version of the day-span procedure? Briefly explain your answer. |
3.
Write a procedure named student-grade that, given a test score between 0 and 100 as argument, returns an indicator of the appropriate grade. (You should use a cond.)
|
If you want to open this code in another window, click here
;; Return the difference in days between earlier-date and ;; later-date. earlier-date and later-date both represent ;; dates in 2002, with earlier-date being the earlier of ;; the two dates. ;; Note: general-day-span is not implemented. (define (day-span earlier-date later-date) (cond ((same-month? earlier-date later-date) (same-month-span earlier-date later-date) ) ((consecutive-months? earlier-date later-date) (consec-months-span earlier-date later-date) ) (else (general-day-span earlier-date later-date) ) ) ) ;; Selector procedures for the components of a date. (define (month-name date) (first date)) (define (date-in-month date) (first (butfirst date))) ;; Return true if date1 and date2 are dates in the same ;; month, and false otherwise. Date1 and date2 both ;; represent dates in 2002. (define (same-month? date1 date2) (equal? (month-name date1) (month-name date2))) ;; Return the number of the month with the given name. (define (month-number month) (cond ((equal? month 'january) 1) ((equal? month 'february) 2) ((equal? month 'march) 3) ((equal? month 'april) 4) ((equal? month 'may) 5) ((equal? month 'june) 6) ((equal? month 'july) 7) ((equal? month 'august) 8) ((equal? month 'september) 9) ((equal? month 'october) 10) ((equal? month 'november) 11) ((equal? month 'december) 12) ) ) ;; Return true if date1 is in the month that immediately ;; precedes the month date2 is in, and false otherwise. ;; Date1 and date2 both represent dates in 2002. (define (consecutive-months? date1 date2) (= (month-number (month-name date2)) (+ 1 (month-number (month-name date1))) ) ) ;; Return the difference in days between earlier-date and ;; later-date, which both represent dates in the same month ;; of 2002. (define (same-month-span earlier-date later-date) (+ 1 (- (date-in-month later-date) (date-in-month earlier-date)) ) ) ;; Return the number of days in the month named month. (define (days-in-month month) (cond ((equal? month 'january) 31) ((equal? month 'february) 28) ((equal? month 'march) 31) ((equal? month 'april) 30) ((equal? month 'may) 31) ((equal? month 'june) 30) ((equal? month 'july) 31) ((equal? month 'august) 31) ((equal? month 'september) 30) ((equal? month 'october) 31) ((equal? month 'november) 30) ((equal? month 'december) 31) ) ) ;; Return the number of days remaining in the month of the ;; given date,including the current day. Date represents a ;; date in 2002. (define (days-remaining date) (+ 1 (- (days-in-month (month-name date)) (date-in-month date) ) ) ) ;; Return the difference in days between earlier-date and ;; later-date, which represent dates in consecutive months ;; of 2002. (define (consec-months-span earlier-date later-date) (+ (days-remaining earlier-date) (date-in-month later-date))) |
Draw a call treeA call tree for a program is a diagram that contains the names of all the program's procedures, with an arrow from one procedure name to another if the first procedure calls the second. Here, for example, is a call tree for version 2 of the "Difference Between Dates" code.day-span | | v day-of-year / | \ / | \ v | \ days-preceding | \ v \ month-name v date-in-monthOn paper, draw a call tree for the version 1 program, and show it to your t.a. |