Thursday, 22 November 2012

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”.

No comments:

Post a Comment