Saturday, 10 November 2012

Assignment #1 Part 2

After the clarify-image issue, I ran into another problem a week later on the same assignment. This time, it was with check-key, a function that is used to stop the clock when the "down" arrow is pressed on the keyboard. We are asked to define the function check-key after being given two check-expect tests:

; check-key : clocktime string -> clocktime
; Check whether "down" pressed to stop clock

(check-expect (check-key (make-clocktime 1 2 3 GO) "up") (make-clocktime 1 2 3 GO))
(check-expect (check-key (make-clocktime 1 2 3 GO) "down") (make-clocktime 1 2 3 STOP))
; !!! fix check-key
(define (check-key c k) c)


First off, I knew that we had performed a similar function in Week 3 with the DrRacket Timer. Looking back to the notes from that day, we had made a similar check-key function that displayed an image of a STOP sign when the "down" arrow is pressed:

; check-key : image string -> image
; Check whether "down" pressed to pass STOPim

(check-expect (check-key SH "up") SH)
(check-expect (check-key SH "down") STOPim)

(define (check-key im k)
  (cond
    [(equal? k "down") STOPim]
    [else im]))

 

Going from this example, I attempted to modify the function to fit the needs of this assignment. In this case, instead of displaying an image, I would just need to change the "s/g" component of the clocktime struct (previously defined) to "STOP". Originally, I had approached this problem by doing the following (note that "equal?" and "key=?" are synonymous in these cases):
(define (check-key c k)
  (cond
    [(key=? k "down")
     (clocktime-s/g STOP)]
    [else c]))


As you may have suspected, this produces an error. I made several more attempts to express a change in the s/g component:

(define (check-key c k)
  (cond
    [(key=? k "down")
     (set-clock 1 2 3 STOP)]
    [else c]))



(define (check-key c k)
  (cond
    [(key=? k "down")
     (make-clocktime-s/g STOP)]
    [else c]))
 


Eventually, I sought help from Professor Heap in his office hours. He explained that the condition expects a clocktime, represented by c. Similar to the DrRacket Timer example, (define (check-key im k) expects an image, represented by im and the function is successful because STOPim is in fact, an image! In order for the check-key in this assignment to work, I needed to create a condition statement that took the entire clocktime struct into account, instead of the s/g component alone:

; check-key : clocktime string -> clocktime
; Check whether "down" pressed to stop clock

(check-expect (check-key (make-clocktime 1 2 3 GO) "up") (make-clocktime 1 2 3 GO))
(check-expect (check-key (make-clocktime 1 2 3 GO) "down") (make-clocktime 1 2 3 STOP))
; !!! fix check-key
(define (check-key c k)
  (cond
    [(key=? k "down")
     (set-clock (clocktime-hours c) (clocktime-minutes c) (clocktime-seconds c) STOP)]
    [else c]))



This definition of check-key turned out to be functional and both check-expect tests were passed!

For your benefit, the DrRacket Timer example can be found on the CSC104 website under Week 3 at: http://www.cdf.toronto.edu/~heap/104/F12/
Also, if you are struggling with assignments like myself, consulting with the professor is not as scary as it seems. Professor Heap is extremely approachable and helpful and you can find him during office hours 5-6pm on Wednesdays!

No comments:

Post a Comment