00001 #include "MusimatChapter9.h" 00002 MusimatChapter9Section(C091201b) { 00003 Print("*** Palindrome ***"); 00004 /***************************************************************************** 00005 00006 Palindrome 00007 00008 We can iterate a sequence in prime order until the last element in the sequence is 00009 reached, then iterate the sequence retrograde until the first element in the sequence is reached, then 00010 repeat. 00011 *****************************************************************************/ 00012 para1(); // Step into this function to continue. 00013 } 00014 00015 Integer palindrome(IntegerList L, Integer Reference pos, Integer Reference inc) { 00016 Integer curPos = pos; 00017 Integer x = cycle(L, pos, inc); 00018 If (curPos + inc != pos){ 00019 inc = inc * (-1); // change direction 00020 pos = curPos; 00021 } 00022 Return(x); 00023 } 00024 00025 Static Void para1() { 00026 /***************************************************************************** 00027 This method calls cycle() to do most of its work. Like cycle(), this method updates pos, 00028 but it also must update its increment argument, inc, because whenever it hits the end of the list, 00029 we want it to reverse the direction of traversal rather than start over. The extra work done by this 00030 method is to change the increment and reset the position when either end of the list is reached. Here 00031 is an example of invoking palindrome(). 00032 *****************************************************************************/ 00033 00034 Print("*** Palindrome ***"); 00035 IntegerList L(10, 11, 12); 00036 Integer myPos = 0; 00037 Integer myInc = 1; // can be any positive or negative integer 00038 00039 For (Integer i = 0; i < 2 * Length(L); i = i + 1) { 00040 Print(palindrome(L, myPos, myInc)); 00041 } 00042 00043 /***************************************************************************** 00044 prints 10, 11, 12, 12, 11, 10. Note that the end of the list is printed twice. This makes it a 00045 so-called even palindrome. It would be an odd palindrome if it were 10, 11, 12, 11, 10. It is left 00046 as an exercise for the reader to adapt palindrome() to generate odd palindromes. 00047 00048 *****************************************************************************/ 00049 } 00050 00052 /* $Revision: 1.3 $ $Date: 2006/09/05 08:02:45 $ $Author: dgl $ $Name: $ $Id: C091201b.cpp,v 1.3 2006/09/05 08:02:45 dgl Exp $ */ 00053 // The Musimat Tutorial � 2006 Gareth Loy 00054 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" � 2006 Gareth Loy 00055 // and published exclusively by The MIT Press. 00056 // This program is released WITHOUT ANY WARRANTY; without even the implied 00057 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00058 // For information on usage and redistribution, and for a DISCLAIMER OF ALL 00059 // WARRANTIES, see the file, "LICENSE.txt," in this distribution. 00060 // "Musimathics" is available here: http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916 00061 // Gareth Loy's Musimathics website: http://www.musimathics.com/ 00062 // The Musimat website: http://www.musimat.com/ 00063 // This program is released under the terms of the GNU General Public License 00064 // available here: http://www.gnu.org/licenses/gpl.txt 00065