public class lec09_v1 extends schemecompat { /* CODE RELATED TO is-prefix-list? AND is-prefix-string? TASKS */ static boolean isDigitChar( Object c ) { return ("0123456789".indexOf( ((Character)c).charValue() ) != -1); } static boolean isPrefixString( Object s ) { return isPrefixList( stringToList(s) ); } static boolean isPrefixList( Object x ) { Object afterExp = removeOnePrefixExp( x ); return isNull( afterExp ); } static Object removeOnePrefixExp( Object l ) { if (isNull( l )) return falseObj; else { if (isDigitChar(car(l))) { return cdr(l); } else if (car(l).equals(new Character('-'))) { Object afterMinus = cdr(l); Object afterExp1 = removeOnePrefixExp(afterMinus); Object afterExp2 = (( ! afterExp1.equals(falseObj)) ? removeOnePrefixExp( afterExp1 ) : falseObj ); return afterExp2; } else { return falseObj; } } } /* TESTS START HERE */ static void test(String name, Object result, Object expected) { if ((! result.equals(expected))) { display("TEST FAILURE: "); display(name); display(" result: "); display(result); display(" should be: "); display(expected); newline(); } else { display("test success: "); display(name); newline(); } } static void testBool(String name, boolean result, boolean expected) { if (result != expected) { display("TEST FAILURE: "); display(name); display(" result: "); display(result); display(" should be: "); display(expected); newline(); } else { display("test success: "); display(name); newline(); } } /* DEFINITIONS FOR TEST INPUTS */ static Object // (#\1) list1 = cons( digits[1], empty), // (#\2 #\3) list23 = cons( digits[2], cons( digits[3], empty )), // (#\3) list3 = cons( digits[3], empty ), // (#\- #\2 #\1 #\4) list_214 = cons( minusChar, cons( digits[2], cons( digits[1], cons( digits[4], empty )))), // (#\4) list4 = cons( digits[4], empty ), // (#\- #\9 #\5) list_95 = cons( minusChar, cons( digits[9], cons( digits[5], empty ))), // (#\- #\- #\2 #\1 #\- #\9 #\5) list__21_95 = cons( minusChar, cons( minusChar, cons( digits[2], cons( digits[1], list_95)))), // (#\- #\1 #\- #\2 #\3) list_1_23 = cons( minusChar, cons( digits[1], cons( minusChar, list23 ))), // (#\- #\- #\- #\2 #\1 #\- #\9 #\5 #\- #\1 #\- #\2 #\3) list___21_95_1_23 = cons( minusChar, append( list__21_95, list_1_23 )), // (#\- #\- #\- #\2 #\1 #\- #\9 #\5 #\- #\1 #\- #\2 #\3 #\6) list___21_95_1_236 = append( list___21_95_1_23, cons( digits[6], empty )), // (#\- #\n #\d) list_nd = cons( minusChar, cons( new Character('n'), cons( new Character('d'), empty ))) ; public static void main(String[] args) { test("remove-1", removeOnePrefixExp( list23 ), list3 ); test("remove-2", removeOnePrefixExp( list_214 ), list4 ); test("remove-3", removeOnePrefixExp( list__21_95 ), empty ); test("remove-4", removeOnePrefixExp( empty ), falseObj ); testBool("plst?-0", isPrefixList( empty ), false ); testBool("plst?-1", isPrefixList( list1 ), true ); testBool("plst?-2", isPrefixList( list__21_95 ), true ); testBool("plst?-3", isPrefixList( list___21_95_1_23 ), true ); testBool("plst?-4", isPrefixList( list___21_95_1_236 ), false ); testBool("plst?-5", isPrefixList( list_nd ), false ); testBool("pstr?-0", isPrefixString( "" ), false ); testBool("pstr?-1", isPrefixString( "1" ), true ); testBool("pstr?-2", isPrefixString( "--21-95" ), true ); testBool("pstr?-3", isPrefixString( "---21-95-1-23" ), true ); testBool("pstr?-4", isPrefixString( "---21-95-1-236" ), false ); testBool("pstr?-5", isPrefixString( "-nd" ), false ); } }