;;Homework 4 solutions ;;;;;;;;;;;;;;; ;;Question 1 ;; ;;;;;;;;;;;;;;; (define (feet dist) (first dist)) (define (inches dist) (first (bf dist))) (define (height-difference dist1 dist2) (if (<= (inches dist2) (inches dist1)) (se (- (feet dist1) (feet dist2)) (- (inches dist1) (inches dist2))) (se (- (feet dist1) (feet dist2) 1) (- (+ (inches dist1) 12) (inches dist2))))) (define (height-difference2 dist1 dist2) (let ((dist1inches (+ (* (feet dist1) 12) (inches dist1))) (dist2inches (+ (* (feet dist2) 12) (inches dist2)))) (se (quotient (- dist1inches dist2inches) 12) (remainder (- dist1inches dist2inches) 12)))) ;The first solution is closer to the incorrect deadenddates solution in the case study. This is because instead of converting each distance into something more comparable, we just try to check all special cases to find the distance between the two distances. ;The second solution is closer to the correct solution in the difference between dates case study. This is because we convert our (feet inches) data type into something that can be more easily compared: inches. Now we can find the difference just by subtracting inches from inches. This is akin to converting (month day) into just days in the case study. ;The first method worked for this problem but not in the case study because you always know how many inches are in a foot while you don't know how many days are in a given month. So we can calcuate that the number of inches between two distances four feet apart, 4x12=48, but we don't know the number of days between two dates that are four months apart. ;;;;;;;;;;;;;;; ;;Question 2 ;; ;;;;;;;;;;;;;;; ; Return the number of days spanned by earlier-date and later-date. ; Earlier-date and later-date both represent dates in 1994, ; with earlier-date being the earlier of the two. ; (define (days-between earlier-date later-date) (+ 1 (- (day-of-century later-date) (day-of-century earlier-date))) ) ; Return the number of days from January 1, 1901 to the given date, inclusive. ; Date represents a day between January 1, 1901 and December 31,1999. ; (define (day-of-century date) (+ (days-preceding-year (year-in-century date)) (days-preceding-month (month-name date) (year-in-century date)) (date-in-month date))) ; Return the number of days from January 1, 1901 ; to the first day of the year named year. ; This can be calculated as 365 times years before ; plus the number of leap years before. (define (days-preceding-year year) (+ (* 365 (- year 1)) (quotient (- year 1) 4))) ; Return the number of days from January 1 to the first day ; of the month named month in the year given by year. (define (days-preceding-month month year) (if (leap-year? year) (item (month-number month) '(0 31 60 91 121 152 182 213 244 274 305 335)) (item (month-number month) '(0 31 59 90 120 151 181 212 243 273 304 334)))) ; Return true if year is a leap year. (define (leap-year? year) (= (remainder year 4) 0)) ; Access functions for the components of a date. (define (month-name date) (first date)) (define (date-in-month date) (first (bf date))) (define (year-in-century date) (first (bf (bf date)))) ; 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) ) ) _________________________________________________________________ email comments or corrections to AJ at cs3-tf@po.eecs.berkeley.edu