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.




                     

No comments:

Post a Comment