Saturday, November 30, 2013

Chapter 4: Second Day of Project #2 (Fx)

            Today is my second of finishing the project #2. I have finished the 2 problem in one day since I have learned something from the Racket video and website Racket documents.
            For the first problem, fx.rkt, the most important and difficult part of the question is to define the "toggle-band". Since this is the key press expression which we don't have video on the course website. However, the logics about the definition was very similar to the conditional commands. According to the given check expectations, it is easily to figure three function keys, "r" "g" "b". Each of them exert the function of averaging the colour. However, the question is how to right three things in one definition statements. The conditional command would be useful to do that. Because I can assume the key expression k equals to "r", then right on the job if it does. As following:
(define (toggle-band ac k)
    (cond
      [(equal? k "r")
       (cond
         [(avcol-r ac) (make-avcol false (avcol-g ac) (avcol-b ac))]
         [else (make-avcol true (avcol-g ac) (avcol-b ac))])]
But this is only one of three parts of the definition as we have three keys to define. I opened another window of DrRacket to test my definition and then cut and paste to the fx file. This problem did not cost me too much time, because the formula of the definition was fixed. All I need to do is changing the variables in the formula to achieve the goal. Since the assignment is due, I can post my whole definition for the record,
(define (toggle-band ac k)
    (cond
      [(equal? k "r")
       (cond
         [(avcol-r ac) (make-avcol false (avcol-g ac) (avcol-b ac))]
         [else (make-avcol true (avcol-g ac) (avcol-b ac))])]
      [(equal? k "g")
       (cond
         [(avcol-g ac) (make-avcol (avcol-r ac) false (avcol-b ac))]
         [else (make-avcol (avcol-r ac) true (avcol-b ac))])]
      [(equal? k "b")
       (cond
         [(avcol-b ac) (make-avcol (avcol-r ac) (avcol-g ac) false)]
         [else (make-avcol (avcol-r ac) (avcol-g ac) true)])]
      [else (make-avcol (avcol-r ac) (avcol-g ac) (avcol-b ac))]
  ))

No comments:

Post a Comment