LAB SOLUTION 4 (written by Luan Nguyen)

advertisement
LAB SOLUTION 4 (written by Luan Nguyen)
Exercise 1:
Dates.deadend.scm
a) There are many different solutions for this part, just remember that the total days for a
month can be either 30 or 31 (except February).
: (day-span '(january 1) '(February 14))
45
: (day-span '(february 1) '(March 17))
45
: (day-span '(april 1) '(may 15))
45
b)
 Since dates.deadend.scm does not handle the general-day-span, -ie we can not find
the difference days between two arbitrary months (not consecutive months).
: (day-span '(january 1) '(december 31))
*** ERROR IN day-span -- Unbound variable: general-day-span
 This is the correct call (a little bit tricky)
: (day-span '(July 1) '(August 31))
62
 However, we can have a large value, but it does not make sense such as
: (day-span '(July 1) '(August 300))
331
why’s that? Computer is stupid!
c) Some of you may think that if you enter two identical dates, the function would return
0.
: (day-span '(July 1) '(July 1))
1

Why? Because if you look at the code, you will find down (+ 1 …) => automatically
add 1 to the result
(define (same-month-span earlier-date later-date)
(+ 1
(- (date-in-month later-date) (date-in-month earlier-date)) ) )

Here is the correct call ( as long as the second date is lesser than the first dates one
day).
: (day-span '(July 2) '(July 1))
0
date1.scm
a) same as the previous part
: (day-span '(january 1) '(February 14))
45
: (day-span '(february 1) '(March 17))
45
: (day-span '(april 1) '(may 15))
45
c)
 This is the expected solution
: (day-span '(january 1) '(december 31))
365
 However, you may get a larger value if you call something like this
:(day-span '(february 1) '(december 100))
403
BUT it does not make sense (bugs)
d) same as the previous part (+ 1 …)
(define (day-span earlier-date later-date)
(+ 1 (- (day-of-year later-date) (day-of-year earlier-date))) )
 Here is an example call
: (day-span '(december 2) '(december 1))
0
Exercise 2:
 Remember that an index of a list always starts at 0. There are two possible solutions
(define (my-days-in-month month)
(list-ref
'(0 31 28 31 30 31 30 31 31 30 31 30 31)
(month-number month)))
(define (my-days-in-month month)
(list-ref
'(31 28 31 30 31 30 31 31 30 31 30 31)
(- (month-number month) 1)))
: (my-days-in-month 'january)
31
: (my-days-in-month 'June)
30
 There are also two possible solutions for this part
(define (my-days-preceding month)
(list-ref
'(0 0 31 59 90 120 151 181 212 243 273 304 334)
(month-number month)))
(define (my-days-preceding month)
(list-ref
'(0 31 59 90 120 151 181 212 243 273 304 334)
(- (month-number month) 1)))
: (my-days-preceding'january)
0
: (my-days-preceding 'June)
151
: (my-days-preceding'december)
334
Exercise 3:

You need to use the Replacement Modeler to determine the sequence of
evaluations that lead to a crash
Dates.deadend.scm
a)
: (day-span '(january) '(February))
*** ERROR -- PAIR expected
(cadr '(january))
b)
: (day-span '(jan 2) '(Feb 3))
*** ERROR -- NUMBER expected
(+ 1 '#[undefined])
c)
: (day-span '(january 2.3) '(February 3.2))
32.9
d)
: (day-span '(january a) '(February b))
*** ERROR -- NUMBER expected
(- 31 'a)
e)
: (day-span '(january 2 a) '(February 4 b))
34
dates1.scm (error messages may be different from the previous part)
a)
: (day-span '(january) '(February))
*** ERROR -- PAIR expected
(cadr '(february))
b)
: (day-span '(jan 2) '(Feb 3))
*** ERROR -- NUMBER expected
(+ '#[undefined] 3)
c)
: (day-span '(january 2.3) '(February 3.2))
32.900000000000006
d)
: (day-span '(january a) '(February b))
*** ERROR -- NUMBER expected
(+ 31 'b)
e)
: (day-span '(january 2 a) '(February 4 b))
34
Exercise 4:
TA should make at least one runtime error and one syntax error for them to debug! You
may ask them which one is easier to debug => should test their functions carefully before
using them in other functions to avoid runtime errors.
Download