Sieve of Eratosthenes in Java

Your task: Redo the "Sieve of Eratosthenes" problem that we did in Scheme, using lazy lists, in Java. We will use the class Object to represent various kinds of lazy lists. Since Java does not support first-class functions, we'll use a similar technique as in the above problem. We will use an interface as above to contain a function:

interface LazyListFct
{
  LazyList f();
}

All implementations of LazyListFct will simply redefine f to be a particular function. For example, here is an excerpt from the infSeq function that you will write:

class LazyList
{
  ...
  public static LazyList infSeq(final int start)
  { 

    class InfSeqTail implements LazyListFct
    {
      public LazyList f()
      {
        return infSeq(start+1);
      }
    } // end of inner-class definition

    // The code for infSeq goes here, using the above class
    ...
  }
  ...
}

Whenever a particular function must be passed as a value, an instance of an implementation of LazyListFct is used.

The class you need to build, LazyList, is structured as:

class LazyList
{
  private Object head;       // Head of this list, null if list is empty
  private LazyListFct tail;  // Function to compute tail of this list
                             // (if head is not null)
  public LazyList()          // Default constructor for LazyList
  {
    head = null;
  }
  public LazyList(Object head, LazyListFct tail)
  {
    // Another constructor
    this.head = head;
    this.tail = tail;
  }
  public static LazyList seq(final int start, final int finish)
  {
    // Same definition as in previous assignment
  }
  public static LazyList infSeq(final int start)
  {
    // Same definition as in previous assignment
  }
  public Object nth(int n)
  {
    // Same definition as in previous assignment
    // EXCEPT that it returns null if if n-th element does not exist
  }
  public void printN(int n)
  {
    // Like firstN except that values are printed
    // rather than formed into a list.
  }
  public static LazyList filterMultiples(final int factor,
                                         final LazyList data)
  {
    // Same definition as in previous assignment
  }
  public static LazyList primes()
  {
    // Same definition as in previous assignment
  }

  public static void main(String[] args)
  {
    // Create a lazy list and test that it works
    LazyList primes = LazyList.primes();
    primes.printN(15);
    System.out.println(primes.Nth(20));
  }

}

You could accomplish the same behavior in a slightly more confusing but considerably more condensed format using anonymous inner classes. After you get your code working, create another class called LazyListAnon that redoes all of the above with anonymous inner classes.


Submit your code in files called LazyList.java and LazyListAnon.java.