class Nil extends Exception { } class Cons extends Exception { private Object x; private ListThunk s; public Cons(Object x, ListThunk s) { this.x = x; this.s = s; } public Object x() { return x; } public ListThunk s() { return s; } } interface ListThunk { public void s() throws Nil, Cons; } interface Transform { public Object f(Object x); } class Map implements ListThunk { private Transform f; private ListThunk l; public Map(Transform f, ListThunk l) { this.f = f; this.l = l; } public void s() throws Nil, Cons { try { l.s(); } catch (Nil n) { throw n; } catch (Cons c) { throw new Cons(f.f(c.x()), new Map(f, c.s())); } } } class Cycle implements ListThunk { private Object x; public Cycle(Object x) { this.x = x; } public void s() throws Cons { throw new Cons(x, this); } } public class Nightmare { public static void print(ListThunk l) { try { l.s(); } catch (Nil n) { } catch (Cons c) { System.out.println(c.x()); print(c.s()); } } public static void main(String args[]) { ListThunk l = new Map(new Transform() { public Object f(Object x) { return ((String)x).toLowerCase(); } }, new Cycle(args[0])); print(l); } }