Sunday, 25 November 2012
Exam Preparation
By far, the most challenging thing about CSC104 is the theoretical portion on the history and applications of computers. Having written the 2nd term test not too long ago, I am now fully aware of my weak points, which are essentially most things besides DrRacket. As the exam approaches, I will be trying my hardest to attend the lectures and make useful notes. I also intend to take advantage of the available office hours and tutorials to get help from Professor Heap and my TAs. Strategies in my other courses that I plan to apply to CSC104 are making a study group with my classmates so that we can reinforce our understandings. As I have previously mentioned, Professor Heap posts extra notes on the CSC104 homepage which have proven to be quite useful when preparing for tests. I hope to combine all these strategies to do my best in the upcoming exam.
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!
; 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")
[else c]))
(define (check-key c k)
(cond
[(key=? k "down")
[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!
Saturday, 3 November 2012
Assignment #1
Fortunately for me, I found almost everything to be pretty straightforward this week. The biggest challenge I encountered was from our first project that is due next week.
In DrRacket we are given instructions to complete the definition of a function, clarify-image that will take an image and make its light colours transparent. We are given the definition of clarify-color which takes the sum of the red, green and blue colour intensities of a pixel and sets their alpha intensity to 0 depending if the sum is greater than 650. Pixels with colour intensities over 650 will become absolutely transparent, otherwise, they remain unchanged. The definition of clarify-color is given as follows:
; clarify-color : color -> color
; Make alpha 0 for colors with r+g+b > 650, leave other colors as-is
(define (clarify-color c)
(make-color
(color-red c)
(color-green c)
(color-blue c)
(cond
[(> (+ (color-red c) (color-green c) (color-blue c)) 650) 0]
[else (color-alpha c)])))
The next portion of this code asks to write a definition for clarify-image, having been given 2 check-expects:
; clarify-image : image -> image
; Make alpha 0 for colors with r+g+b > 650, leave other colors as-is, in im
(check-expect (clarify-image (circle 10 "solid" (make-color 250 250 250 255)))
(circle 10 "solid" (make-color 250 250 250 0)))
(check-expect (clarify-image (circle 10 "solid" (make-color 100 100 100 255)))
(circle 10 "solid" (make-color 100 100 100 255)))
; !!! fix the definition of clarify-image
(define (clarify-image im) im)
This was the hard part. I initially approached this question thinking "I want clarify-image to do exactly what clarify-color does, except to entire images". Copying the exact code of clarify-color and changing "c" to "im" will not work. I know this because I tried it and it failed. And while most check-expects provide clues on writing a definition, these were not very helpful. However, I recalled that during one of our lectures, Professor Heap took an image and made it less green by first defining a function to reduce the green colour and then using map-image to use that function towards an image. Map-image is important as it applies a function to all pixels of an inputted image. Following the examples Heap gave in class and combining them with my new-found knowledge of condition statements, I was able to devise a definition that carried out the role of clarify-image. At first I had created an additional function (which I later realized was redundant), do-clarify-image that would do what clarify-image is supposed to do on a pixel level. I would then use map-image to apply do-clarify-image to all the pixels in an image:
(define (do-clarify-image c)
(make-color
(color-red c)
(color-green c)
(color-blue c)
(cond
[(> (+ (color-red c) (color-green c) (color-blue c)) 650) 0]
[else (color-alpha c)])))
(define (clarify-image im)
map-image do-clarify-image im)
The check-expect tests were ran and they passed using this definition of clarify-image. It then dawned on me that do-clarify-image was uncannily similar to another function I've seen before...
Yes, that's right.
The definitions for clarify-color and do-clarify-image are exactly the same.
Therefore, you can can completely eliminate do-clarify-image and replace it with clarify-color which has already been defined! You will end up with:
(define (clarify-image im)
map-image clarify-color im)
Although it may seem confusing, this is the actual thought process I used to arrive at the solution!
Below are some helpful links that helped me with this part of the assignment:
You can find the code to Professor Heap's mapping lecture on the CSC course website for Week 5 http://www.cdf.toronto.edu/~heap/104/F12/.
Also, the Picturing Programs Teachpack is useful in explaining the purpose of map-image and make-color:
http://docs.racket-lang.org/picturing-programs/index.html
In DrRacket we are given instructions to complete the definition of a function, clarify-image that will take an image and make its light colours transparent. We are given the definition of clarify-color which takes the sum of the red, green and blue colour intensities of a pixel and sets their alpha intensity to 0 depending if the sum is greater than 650. Pixels with colour intensities over 650 will become absolutely transparent, otherwise, they remain unchanged. The definition of clarify-color is given as follows:
; clarify-color : color -> color
; Make alpha 0 for colors with r+g+b > 650, leave other colors as-is
(define (clarify-color c)
(make-color
(color-red c)
(color-green c)
(color-blue c)
(cond
[(> (+ (color-red c) (color-green c) (color-blue c)) 650) 0]
[else (color-alpha c)])))
The next portion of this code asks to write a definition for clarify-image, having been given 2 check-expects:
; clarify-image : image -> image
; Make alpha 0 for colors with r+g+b > 650, leave other colors as-is, in im
(check-expect (clarify-image (circle 10 "solid" (make-color 250 250 250 255)))
(circle 10 "solid" (make-color 250 250 250 0)))
(check-expect (clarify-image (circle 10 "solid" (make-color 100 100 100 255)))
(circle 10 "solid" (make-color 100 100 100 255)))
; !!! fix the definition of clarify-image
(define (clarify-image im) im)
This was the hard part. I initially approached this question thinking "I want clarify-image to do exactly what clarify-color does, except to entire images". Copying the exact code of clarify-color and changing "c" to "im" will not work. I know this because I tried it and it failed. And while most check-expects provide clues on writing a definition, these were not very helpful. However, I recalled that during one of our lectures, Professor Heap took an image and made it less green by first defining a function to reduce the green colour and then using map-image to use that function towards an image. Map-image is important as it applies a function to all pixels of an inputted image. Following the examples Heap gave in class and combining them with my new-found knowledge of condition statements, I was able to devise a definition that carried out the role of clarify-image. At first I had created an additional function (which I later realized was redundant), do-clarify-image that would do what clarify-image is supposed to do on a pixel level. I would then use map-image to apply do-clarify-image to all the pixels in an image:
(define (do-clarify-image c)
(make-color
(color-red c)
(color-green c)
(color-blue c)
(cond
[(> (+ (color-red c) (color-green c) (color-blue c)) 650) 0]
[else (color-alpha c)])))
(define (clarify-image im)
map-image do-clarify-image im)
The check-expect tests were ran and they passed using this definition of clarify-image. It then dawned on me that do-clarify-image was uncannily similar to another function I've seen before...
Yes, that's right.
The definitions for clarify-color and do-clarify-image are exactly the same.
Therefore, you can can completely eliminate do-clarify-image and replace it with clarify-color which has already been defined! You will end up with:
(define (clarify-image im)
map-image clarify-color im)
Although it may seem confusing, this is the actual thought process I used to arrive at the solution!
Below are some helpful links that helped me with this part of the assignment:
You can find the code to Professor Heap's mapping lecture on the CSC course website for Week 5 http://www.cdf.toronto.edu/~heap/104/F12/.
Also, the Picturing Programs Teachpack is useful in explaining the purpose of map-image and make-color:
http://docs.racket-lang.org/picturing-programs/index.html
Subscribe to:
Posts (Atom)