JAVA   74

Nightmare.java

Guest on 2nd September 2021 02:06:12 AM

  1. class Nil extends Exception
  2. {
  3. }
  4.  
  5. class Cons extends Exception
  6. {
  7.   private Object x;
  8.   private ListThunk s;
  9.   public Cons(Object x, ListThunk s) {
  10.     this.x = x;
  11.     this.s = s;
  12.   }
  13.   public Object x() {
  14.     return x;
  15.   }
  16.   public ListThunk s() {
  17.     return s;
  18.   }
  19. }
  20.  
  21. interface ListThunk
  22. {
  23.   public void s() throws Nil, Cons;
  24. }
  25.  
  26. interface Transform
  27. {
  28.   public Object f(Object x);
  29. }
  30.  
  31. class Map implements ListThunk
  32. {
  33.   private Transform f;
  34.   private ListThunk l;
  35.   public Map(Transform f, ListThunk l) {
  36.     this.f = f;
  37.     this.l = l;
  38.   }
  39.   public void s() throws Nil, Cons {
  40.     try {
  41.       l.s();
  42.     }
  43.     catch (Nil n) {
  44.       throw n;
  45.     }
  46.     catch (Cons c) {
  47.       throw new Cons(f.f(c.x()), new Map(f, c.s()));
  48.     }
  49.   }
  50. }
  51.  
  52. class Cycle implements ListThunk
  53. {
  54.   private Object x;
  55.   public Cycle(Object x) {
  56.     this.x = x;
  57.   }
  58.   public void s() throws Cons {
  59.     throw new Cons(x, this);
  60.   }
  61. }
  62.  
  63. public class Nightmare
  64. {
  65.   public static void print(ListThunk l) {
  66.     try {
  67.       l.s();
  68.     }
  69.     catch (Nil n) {
  70.     }
  71.     catch (Cons c) {
  72.       System.out.println(c.x());
  73.       print(c.s());
  74.     }
  75.   }
  76.   public static void main(String args[]) {
  77.     ListThunk l = new Map(new Transform() {
  78.       public Object f(Object x) {
  79.         return ((String)x).toLowerCase();
  80.       }
  81.     }, new Cycle(args[0]));
  82.     print(l);
  83.   }
  84. }

Raw Paste


Login or Register to edit or fork this paste. It's free.