Misato Daniel R Jillian Kefei Patrick Yibo Garrett Iris Tao Santiago 
John Daniel C Nicholas Noah Keiland Elise Khalea Ryan Justin Bihan Jiang


public class HomeworkTen {
  public static void main(String[] args) {
    LispList list = new NonEmptyList("A", 
                      new NonEmptyList("B", 
                        new NonEmptyList("C", 
                          new EmptyList())));
    System.out.println( list );  
    LispList a = new EmptyList();
    System.out.println( a ); 
    LispList b = new NonEmptyList("C", a);
    System.out.println( b );
    LispList c = new NonEmptyList("B", b);
    System.out.println( c ); 
    LispList d = new NonEmptyList("A", c);
    System.out.println( d ); 

    LispList listTwo = LispList.NIL.cons("C").cons("B").cons("A"); 
    System.out.println( listTwo ); 

  }
}

--

public interface LispList {
  Object head(); // first 
  LispList tail(); // rest 
  boolean empty(); // empty? 
  LispList cons(Object object); 
  public static LispList NIL = new EmptyList(); 
  // ... 
}

--

public class EmptyList implements LispList {
  public boolean empty() {
    return true;  
  }
  public LispList tail() {
    return null; // throw Exception 
  }
  public Object head() {
    return null;  // throw Exception 
  }
  public String toString() {
    return ""; 
  }
  public LispList cons(Object object) {
    return new NonEmptyList(object, this);
  }
}

--

public class NonEmptyList implements LispList {
   private Object head;
   private LispList tail; 
   public NonEmptyList(Object head, LispList tail) {
     this.head = head; 
     this.tail = tail; 
   }
   public Object head() {
     return this.head; 
   }
   public LispList tail() {
     return this.tail; 
   }
   public boolean empty() {
     return false;  
   }
   public String toString() {
     String answer = "[" + this.head();
     LispList a = this.tail(); 
     while (! a.empty()) {
       answer = answer + ", " + a.head(); 
       a = a.tail(); 
     }
     return answer + "]"; 
   }
   public LispList cons(Object object) {
     return new NonEmptyList(object, this);
   }
}

--

Then we look at 16.10 and this is the code I came up with:

(a) what to do with empty list when printing

(b) how do we incorporate exceptions in the empty list

Without those the code is:

public interface LispList {
  Object head(); // first 
  LispList tail(); // rest 
  boolean empty(); // empty? 
  LispList cons(Object object); 
  public static LispList NIL = new EmptyList(); 
  public int length(); 
  // ... 
}

--

public class EmptyList implements LispList {
  public boolean empty() {
    return true;  
  }
  public LispList tail() {
    return null; // throw Exception 
  }
  public Object head() {
    return null;  // throw Exception 
  }
  public String toString() {
    return ""; 
  }
  public int length() {
    return 0; 
  }
  public LispList cons(Object object) {
    return new NonEmptyList(object, this);
  }
}

--

public class NonEmptyList implements LispList {
   private Object head;
   private LispList tail; 
   public NonEmptyList(Object head, LispList tail) {
     this.head = head; 
     this.tail = tail; 
   }
   public Object head() {
     return this.head; 
   }
   public LispList tail() {
     return this.tail; 
   }
   public boolean empty() {
     return false;  
   }
   public int length() {
     return 1 + this.tail.length();  
   }
   public String toString() {
     String answer = "[" + this.head();
     LispList a = this.tail(); 
     while (! a.empty()) {
       answer = answer + ", " + a.head(); 
       a = a.tail(); 
     }
     return answer + "]"; 
   }
   public LispList cons(Object object) {
     return new NonEmptyList(object, this);
   }
}

--

public class HomeworkTen {
  public static void main(String[] args) {
    LispList list = new NonEmptyList("A", 
                      new NonEmptyList("B", 
                        new NonEmptyList("C", 
                          new EmptyList())));
    System.out.println( list );  
    LispList a = new EmptyList();
    System.out.println( a ); 
    LispList b = new NonEmptyList("C", a);
    System.out.println( b );
    LispList c = new NonEmptyList("B", b);
    System.out.println( c ); 
    LispList d = new NonEmptyList("A", c);
    System.out.println( d ); 

    LispList listTwo = LispList.NIL.cons("C").cons("B").cons("A"); 
    System.out.println( listTwo ); 
    
    System.out.println( listTwo.length() ); // 3 
    listTwo = listTwo.cons("M"); // [M, A, B, C] 
    
    System.out.println( listTwo ); 
    System.out.println( listTwo.length() ); // 4

  }
}

So after (a) and (b) we should solve 16.12 (contains). 

--