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

...