Wednesday, 31 October 2012

Week 8: About the String Functions


We had to go through the text ourselves and try out the string functions. I found it challenging as these few weeks we have to compose the codes ourselves.

I firstly practiced with the basic codes in order to revise the following three functions:
1. substring
>(substring "tiffany" 0 4)
tiff
2. string-append
>(string-append "tiffany" "is" "a" "good" "girl")
tiffanyisagoodgirl

3. string-length
>(string-length "tiffany")
7
I started working on the tutorial handout afterwards. These are what I got after spending more than an hour on DrRacket.

(define (repeat word)(string-append word word))  
(define (chop-first-char xxx)(substring xxx 1)) 
(define (first-char x)(substring x 0 1)) 
(define (last-half xxxx)(substring xxxx
                                   (round (* 1/2 (string-length xxxx)))))
                                     
(define (first-half xxxx)(substring xxxx 0
                                   (round (* 1/2 (string-length xxxx)))))

these are the outputs:

>(repeat word "tiff")
tifftiff 
>(chop-first-char "tiff")
iff
>(first-char "tiff")
t 
>(last-half "tiff")
ff 
>(first-half "tifff")
ti

This exercise indeed required me think a lot and undoubtedly, a complete understanding of the codes.




                     

Wednesday, 24 October 2012

Week 7: If-else Statements and GCD

This week, I've learned about the if-else statements and the build-in function GCD.

For the conditional statement, I've tried to define some with it.
For example, defining a number greater than 500 as big,

(define (bigger-number n)
  (cond
    [(> n 500) "big"]
    [else "Not too big"])) 

(check-expect (bigger-number 30) Not too big)
(check-expect (bigger-number 555) big)



The greatest common divisor (GCD) of two non-negative integers (whole numbers) is a non-negative integer that
  1. divides both numbers, and
  2. is the largest non-negative number that does so.

For the GCD funtion, here are some examples,

(GCD 2 10)
>2
(GCD 100 10)
>10
It is so much easier and convenient when you can find the GCD on the computer.


Wednesday, 17 October 2012

Week 6: Paper Folding and "make-color"

I tried out with the paper folding. I marked down the folds I got after folding it the left corner to the right corner. It took me quite a while to read through the links and match the patterns with the folding.

For example, (D=DOWN, U=UP)

1st fold: D  (side view: \/ )
2nd fold: DDU (side view: \_/\ )
3rd fold: DDUDDUU
4th fold: DDUDDUUDDDUUDUU

We can also represent the number of down fold vertex found in that number of folds we made by this formula, 2^(n-1) wher n>=1.  
(n= number of times the strip of paper is folded)


1st fold: D=2^(1-1)= 1
2nd fold: D=2^(2-1)=2
3rd fold: D=2^(3-1)=4
4th fold: D=2^(4-1)=8
5th fold: D=2^(5-1)=16

(It is noted that he pattern is the representation of binary number system (base 2 system) where each fold represents the bits and the number of Ds represent the maximum value that bit can hold.)

I spent some time on the codes, the handout mentions the swapping between red, blue and green. 
(check-expect (rotate-red-blue-green c)
      (make-color
          (color-green c)
          (color-blue c)
          (color-red c)
          (color-alpha c)))
; 
(define (rotate-red-blue-green col)

Somehow I still found it pretty hard to relate the color change from the codes. I've assumed that 
- pic's red intensity --> assigned green intensity
- pic's green intensity --> assigned blue intensity
- pic's blue intensity --> assigned red intensity

It's bad that I got this after writing the quiz (if the above is correct).



Sunday, 14 October 2012

Week 5: Reflection of My First Month in CSC104

    Before taking this course, I didn't know anything about computing/programming. All the machines and tools we use every day involve programming and it's amazing to know how these programs work. I've learnt that the pretty pictures are composed by array (glowing dots) and most machines involve boolean. I am also surprised that I can actually do some calculations with programming software. For example,

> (+ 1 2 3 4)
10 
> (< (3 4) or (4 5))
true
> (exact -> inexact #i5.0)
5
> e
#i2.718281828459045
> (round e)
#i3.0

    Since I am a beginner in computer science, I've found that the codes are pretty hard to be interpreted. I have to go through every video at least twice every time and in order to have a better understanding and application, I've played around with DrRocket with the codes and tried working on different combinations of codes. It's pretty interesting sometimes, but whenever I get stuck, I become super confused that I always keep bugging my buddy for help.

    For example, a part of codes in "the rotating fish":

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

Even though the instructions have already been quite clear, it has taken me a while to visualize the process.


    After taking this course for about a month, I appreciate and respect the scientists and inventors a lot more. It's still hard to imagine how much work they had done and gone through. As the systems are written under algorithm, every step has to be clearly stated; it's hard to imagine how many codes the system has run when we are just clicking one single button on the keyboard. Without doubt, it's a bless to be born in this generation and being able to enjoy the convenience brought by our technology, especially after knowing the machines and computers used in the past.