Thursday 29 November 2012

Interesting Wikilinks


  Last week, I finished Wikipedia Project Part #3, which was a very interesting journey. I know that if you add [[  ]] to a word, click that word; you will go to another Wikipedia article. But I did not know that this word must be exactly same as the title of another article. So, for example, when I added [[ ]] to bird, bird would not become a wikilink, because “Bird” is the actual title of the article. Since that, I must write like this: [[Bird|bird]] in order to change “bird” to a wikilink.

  Then, I was also interested in the application: [[Target page#Target section|display text]], which will lead users directly to a specific section of an article. But, I was not sure what I should enter in the “Target section”. So, I tried to enter the number, such as 1.3, showed in the contents first; however, that didn’t work. Then, I tried to enter the subtitle; this time, the wikilink leaded me to that specific section.

I have added many wikilinks in different articles. It was very interesting to check if a special article exists, and add wikilinks to the relevant articles.

Don’t Forget Previous Definitions


      When I carried out all the instructions in Project #2, DrRacket showed a test failed, which was:

(check-expect (mouse-event 5 89 57 "button-down")2)

Then, I used its definition to check again; its definition is:

(define (mouse-event n x y e)

  (cond

    [(equal? e "button-down") (quotient (* x DENOM) (* 3 (image-width p1)))]

[else n]))

But I still got the same answer. I left this question there. After 20 minutes, I suddenly remembered that the width of picture p1 has changed, since the original picture becomes half of it. So, the previous definition

(define p1 (scale 0.5 *))

is the key to solve this problem.

  From this problem, I learn that it is very important to check previous definitions, when you use them to define a new function.

Thursday 22 November 2012

Apply

      When I saw the tutorial handout for the quiz #8, I learnt a new function, called apply. When I first saw the expression:
(apply + (list  3 5 4))
I thought it would have the same effect as the map function, so the above expression would like:
(map add1 (list 1 2 3 4 5))
But, then, just after I wrote down what I thought, I found that, I could not assume + was equal to add1. Therefore, a new list could not be produced. So I tried
(apply + (list 1 2 3 4 5))
in DrRacket, it produced 15. Then I knew it would produce a number by adding all the numbers in the list together.
      Next, I quickly knew the answer of the expression:
(apply + (map string-length (list "one" "three" "five")))
I would have
(apply + (list  3 5 4))
first by using string-length function to “one” “three” “five” to get numbers: 3 5 4. Then 3 5 4 were added together to produce 12. So, 12 was the answer
 

Palindrome?


      When I was reviewing for term test two, I was very confusing the definition expression of palindrome? Especially the else part:

[else

  (and (equal? (substring s 0 1)  (substring s (sub1 (string-length s))))

          (palindrome? (substring s 1 (sub1 (string-length s)))))]

      I did not understand why did palindrome? appear again, and how did it work to the expression. Then I used stepper to see the process step by step. So I knew palindrome?first checks whether the first and the last elements of a string are equal, next checks whether the first and the last elements of remaining unchecked elements are equal, and so on.
     So (palindrome? “huh”) produces true, but  (palindrome? “hsudh”) produces false, since “s” is not equal to “d”.

Test Examples


          When I was doing Project 1, I found I did not understand what the examples in check-expect expressions should look like. For example, when I was answering the first question, I initially wrote down that:

(check-expect (clarify-color (make-color 78 89 98 23))

(make-color  (color-red  (make-color 78 89 98 23))

                      (color-green  (make-color 78 89 98 23))

                      (color-blue  (make-color 78 89 98 23))

                     (cond

                        [(> (+ (color-red  (make-color 78 89 98 23)) (color-green  (make-color 78 89 98 23)) (color-blue  (make-color 78 89 98 23))) 650) 0]

                          [else (color-alpha  (make-color 78 89 98 23))])))

Then, after I finished all the questions, I thought this is wrong. Since this check-expect expression was not different from its definition, though the test passed. Then I changed this check-expect expression to that:

(check-expect (clarify-color (make-color 78 89 98 23))

                        (make-color 78 89 98 23))

      So, I just conclude that usually, check-expect expression is simpler than its definition expression, since the result can be directly written in it.

Sunday 4 November 2012

Place-Holder


          When I was doing Exercise 9.2.2, which requires developing a function named repeat that takes in a string and returns that string appended to itself, I wrote like that:

 

(check-expect  repeat “hello” (string-append “hello””hello”))

 

(define  repeat (string-append “hello””hello”))

 

Then, I found that this new defined function could only be applied to the string “hello”, which was different than what I expected. So, I went to office hour, and asked professor this question. Then I know that I lacked a place-holder, which has a technical name- parameter. So the correct expressions should be:

 

(check-expect(repeat “hello”)(string-append “hello””hello”)

 

(define (repeat string)(string-append string string))

 
   
Then, I tried a different string in Interactions Plane:

 

>(repeat “lol”)

“lollol”

 

          The result shows that the new defined function- repeat- now works for any string.


 

How Could Centre a Model in an Animation?


     The Exercise 8.3.2 asks to modify Exercise 8.3.1 so that the circle appears centered and unmoving in a 200 x 200 white background, so it appears to grow around a fixed center. Initially, I was confused by the method used in Exercise 8.2.3- place-image. But then I was stuck, because the circle is growing, I couldn’t find the exact distance from the left and top-edge for the circle. So, I checked the previous notes, and found a function called overlay, which can put two images together, and let the first image be on the center of the second image. Then I wrote the solution for this exercise:

(check-expect (blue-circle-of-size-center 2)

                      (overlay(circle 2 “solid””blue”)(rectangle 200 200 “solid””white”)))

(check-expect (blue-circle-of-size-center 37)

                      (overlay(circle 37 “solid””blue”)(rectangle 200 200 “solid””white”)))

 

(define(blue-circle-of-size-center radius)

                      (overlay(circle radius “solid””blue”)(rectangle 200 200 “solid””white”)))

 

(big-bang 7 (check-with number?) (on-draw blue-circle-of-size-center) (on-tick add1 1/2))