From: Patrick Prosser Sent: 06 November 2010 22:02 To: zoe_1811@hotmail.com Cc: Patrick Prosser Subject: Palindrome, recursively defined in java Attachments: Palindrome.java Zoe if you have java compile and run the attached, taking a string in the command line Palidrome is defined recursively as follows (i.e. palindrome is defined in terms of palindrome :) s is s palandrome if - s is 1 character long or is empty OR - first and last character of s are the same AND the string s' is a palindrome where s' is the substring of s starting at position 1 and ending at position n-1 Since s' is smaller than s, on each recurisve call s gets smaller and the function terminates with s being of length 1 or being empty, or the first and last characters being different. I think this is neat. It depends on the language dealing with OR (|| in java) correctly, i.e. in evaluating P || Q, correctly. If P is true Q is not evaluated and if P is false Q is evaluated. Also AND (&& in java) must be evaluated correctly, i.e. P && Q, if P is false Q is not evaluated. This may seem small stuff but it is important to understand as it leads to clear thinking!!! :) I love you xxx -- Patrick Prosser tel: +44 141 330 4934 Computing Science fax: +44 141 330 4913 University of Glasgow web: http://www.dcs.gla.ac.uk/~pat/ Glasgow G12 8RZ email: pat@dcs.gla.ac.uk