2012年11月3日星期六

Week 8

     For the lecture, we study rot13 as algorithm. For example: what is a really simple rule (or set of rules) for (rot13 c), where c is some character? The answer is if c is in a-m, add 13. It c is in n-z,  sub 13.
     For Dr,Racket, I have to understand some orders, for example: to produce string with characters in opposite order from s
     (check-expect (string-reverse "string") "gnirts")
;
(define (string-reverse s)
  (cond
    [(zero? (string-length s)) s]
    [else (string-append (string-reverse (substring s 1)) (substring s 0 1))]))

Also, I have to figure out how to explain whether it is a palindrome or not. Firstly I have to write two check-expect steps.
(check-expect (palindrome? "aa") true)
(check-expect (palindrome? "ab") false)

Then define
(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)))))]))

Now I have to prepare for project 1 because it will due next week.


 


没有评论:

发表评论