To your point, here's my Java implementation of the few key Scheme functions we need in actions. It's kind of amusing to see "car" and "cons" in Java :-). Yes, I know Java doesn't guarantee tail-recursion, but for its expected use and environment that shouldn't matter. If we need a few more, we can just add them; this means that we can use Scheme procedures in the ANTLR prototype actions.
--- David A. Wheeler ============================================= package scheme; // (C) 2013 David A. Wheeler, released under MIT license. // Provide a few traditional Lisp-like functions in Java, // just barely enough functionality for sweet.g, but // accurate for what it does. public class Pair extends Object { private Object car_value, cdr_value; public Pair(Object x, Object y) { this.car_value = x; this.cdr_value = y; } public static Pair cons(Object x, Object y) { return new Pair(x, y); } public static Object car(Pair x) { return x.car_value; } public static Object cdr(Pair x) { return x.cdr_value; } public static Boolean nullp(Object x) { // Scheme "null?" return x == null; } public static Boolean pairp(Object x) { // Scheme "pair?" return x instanceof Pair; } // This does not check for cycles private static Boolean ends_in_null(Pair x) { if (nullp(cdr(x))) { return true; } else if (! pairp(cdr(x))) { return false; } else { return ends_in_null( (Pair) cdr(x)); } } public static Boolean listp(Object x) { // Scheme "list?" if (nullp(x)) return false; else if (! pairp(x)) return false; else return ends_in_null( (Pair) x); } public static Pair list(Object x) { return cons(x, null); } public static Pair list(Object x, Object y) { return cons(x, list(y)); } // "y" really needs to be a pair, but that makes it harder to // to work with ANTLR public static Object append(Object x, Object y) { if (x == null) { return y; } else if (x instanceof Pair) { Pair p = (Pair) x; return cons(car(p), append(cdr(p), y)); } else { throw new Error(); // for now. } } private static String string_datum_tail(Object x) { if (x == null) { return ")"; } else if (x instanceof Pair) { Pair p = (Pair) x; return " " + string_datum(car(p)) + string_datum_tail(cdr(p)); } else return " . " + string_datum(x) + ")"; } public static String string_datum(Object x) { if (x == null) { return "()"; } else if (x instanceof Pair) { Pair p = (Pair) x; return "(" + string_datum(car(p)) + string_datum_tail(cdr(p)); } else { return x.toString(); } } // (define (monify x) // (cond // ((not (pair? x)) x) // ((null? (cdr x)) (car x)) // (#t x))) public static Object monify(Object x) { if (! pairp(x)) { return x; } else if (nullp(cdr( (Pair) x))) { return car( (Pair) x); } else {return x;} } } ------------------------------------------------------------------------------ Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS, MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft MVPs and experts. ON SALE this month only -- learn more at: http://p.sf.net/sfu/learnmore_122712 _______________________________________________ Readable-discuss mailing list Readable-discuss@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/readable-discuss