Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Write a (functional) Java program that constructs primes, the lazy inifinite stream of prime numbers 2, 3, 5, 7, ... where numbers are represented using Java type int.  Ignore the fact that the int type only supports integer values less than 2^31.  We will only compute the first million or so primes (about 2^20)few thousand primes, so no one will notice.  Obviously such a program could easily be generalized to use type long or the unbounded integer type java.math.BigInteger.  The support file primeSieve.java provides the interface IntStream which is the root of a composite class hierarchy supporting lazy streams of int, including  methods to print finite ones in conventional Lisp-like notation.  The formulation of streams supported in Java starting with Java 8 is not functional.  Simple operations like extracting the first element of a stream or the length of a finite stream destroy the stream.  Consequently, we must develop our own IntStream library from scratch.  Fortunately, it is not very difficult and requires comparatively little code.  The equivalent program in Haskell (generalized to unbounded integers) is simply

primes = filterPrime [2..]
  where filterPrime (p:xs) =
          p : filterPrime [x | x <- xs, x mod `mod` p /= 0]

Of course, Haskell has built-in support for lazy streams and the recursive definition of functions (like filterprime) using pattern matching all supported by an aggressive optimizing compiler.  Your task is to extend the provided code to support a static final field called primes in the top-level interface IntStream bound to the lazy infinite stream of primes (ignoring the fact that int arithmetic will overflow when numbers get too large.  You also need to augment the JUnit test file (compatible with JUnit 4 as provided by DrJava) IntStreamTest.java to further test your code.  You may assume that all of the provided code is correct. [Please tell us if you discover any bugs!]  You do not need to test any of the methods in the IntStream interface provided by the original version of primeSieve.java.

...

Inside the svn directory https://svn.rice.edu/r/comp311/turnin/F20/netid/, create a directory HaskellProject and put the modified files for Simplifier.hs and Tests.hs. For example, since Agnishom's netid is ac132, he would create https://svn.rice.edu/r/comp311/turnin/F20/ac132/HaskellProject/ and put his files there. You should not need to upload any other files, since these are the only two files you will need to modify. If you do need to include anything else; please ask course staff if you have any questions in this regard.  Do not create any subfolders. Please use the exact path given here.

Honor Code

The final project is conducted under the same honor code as our programming assignments.  You may ask the staff questions and/or post questions on Piazza about the conceptual issues in the projects but no sharing of code with other people (students and non-students) or copying of code from any source, notably reference books and the internet, is permitted.  In general, we prefer that you ask questions on Piazza so that all students in the class can see the answer.