Saturday, November 3, 2012

Better Preparation

The tutorial quizzes are getting harder these past two weeks, so I thought that today I would critically analyze each section of the function of what was taught this week.

So first we have rot13 

(define (rot13 c)
  (cond
    [(char<=? #\A c #\M) (integer->char (+ 13 (char->integer c)))]
    [(char<=? #\a c #\m) (integer->char (+ 13 (char->integer c)))]
    [(char<=? #\N c #\Z) (integer->char (- (char->integer c) 13))]
    [(char<=? #\n c #\z) (integer->char (- (char->integer c) 13))]
    [else c]))



If I were to enter (rot13 #\A), DrRacket would give me #\N, and if I were to enter (rot13 #\w), DrRacket would give me #\j.

Since #\A is within the category of #\A and #\M, DrRacket will read the character as an integer, and add 13 to the "integer", which DrRacket would then convert it back to character, hence the output is #\N. On the other hand, the input of #\w is withing #\n and #\z, hence instead of adding, we get subtracting 13. I also wondered what would happen if I were to switch the order of the last two subtraction conditions like so:


    [(char<=? #\N c #\Z) (integer->char (- 13 (char->integer c)))]
    [(char<=? #\n c #\z) (integer->char (- 13 (char->integer c)))]


...and well, I got an error message. It is logical though, subtracting 5 from 13 and 13 from 5 are two different answers.

Moving on, we have our next function rot13string

 (define (rot13string s)
  (list->string
   (map rot13
        (string->list s))))



Adding map would apply the function to each character which is what we want because this function is to exchange each letter according to what rot13 does. Thus this function takes in a string, changes it to a list to add 13 to each integer, then taking the list it makes a string with the appropriate changes.



Next we have reverse-string

 (define (string-reverse s)
  (cond
    [(zero? (string-length s)) s]
    [else (string-append (string-reverse (substring s 1)) (substring s 0 1))]))
(string-reverse "five")



And it does exactly what the name implies, it takes in a string and reverses it, in other words if I enter in "five" it would give me "evif". 

Its first condition is if the string-length of s (place-holder) is zero then the output would be nothing (hence no reversal will occur). I'm assuming that the use of this condition is that in order to use "cond" you have to have at least 2 conditions. And so I got curious of what would happen if I just eliminated the whole condition parameter like so: 

 (define (string-reeverse s)
   (string-append (string-reverse (substring s 1)) (substring s 0 1)))


...and as I predicted, it worked just fine. 

 Anyways, the second condition applies to all strings that are greater than zero characters. 

Finally we have palindromic

(define (palindrome? s)
  (cond
    [(< (string-length s) 2) true]
    [else
     (and
      (palindrome? (substring s 1 (sub1 (string-length s))))
      (equal? (string-ref s 0) (string-ref s (sub1 (string-length s)))))]))

 

The first condition describes that if the string length is less than 2, it is true that the string is palindromic. The second condition would apply to all those string lengths that are 2 or greater. It would take out the the beginning and last character and compare them to see if they are equal.


Hopefully, I'm better prepared for the next quiz after analyzing these functions more closely!

No comments:

Post a Comment