rex to Java
rex2java

James Gosling,
Inventor of Java
Java,
an Imperative Language
Imperative languages often permit the use of functional programming.
Sometimes just say ÒnoÓ to side-effects.
Otherwise use functions and side-effects articulately.
Best of both worlds!

Java vs. rex
The analog to function in rex is method in Java.  Functions are applied as aFunction(x, y, z), while methods are applied like x.aMethod(y, z).
Argument and return types must be declared in Java, not in rex.
Both allow recursion.
All of the underlying functionality in rex is implementable.
Think of rex lists as your abstraction, use Java to implement it.

Java review
The empty Java program
The empty Java program
The ÒHello, worldÓ program in Java
The ÒHello, worldÓ program in Java
Running Java on turing
Current version is 1.3.1
To compile:

javac Hello.java
To execute:

java Hello

Running Java on turing
HMC Shortcuts
Java Objects
Java data items are either:
Primitives, such as
int, long, float, double, char
Objects, such as
String, Long, Double
Objects you define
Arrays are essentially Objects too.

Purposes of Objects
Aggregate various data objects together
Allow mutation of the state of data objects
Control use and access of data according to specific disciplines
and other good stuff

Immutable Objects
An Object is immutable if its state never changes once it is created.
Functional programming deals with immutable objects almost exclusively
(exception: delayed evaluation)
The aggregating and disciplined access properties of Objects are still very useful.

OpenList class
This is a class you will construct and own.
It will allow you to solve the Unicalc problem in Java, as well as other things.
Think of rex lists as the abstraction.
Use Java to implement.

Using Your OpenList to
Implement Unicalc Functionality
We want to represent Unicalc Quantities.
In Java, instead of using a list of 3 things, we will add a little more structure:
public class Quantity
{
private double Factor;
private OpenList Num;
private OpenList Denom;
É more stuff to come É
}

Object Creation
Objects are created using constructors.
For a given Class of Objects, there can be multiple types of constructors, each providing different types of parameters to define the creation of an object.

Constructors for Quantities
Constructors always take the same name as their Class.
Therefore, all constructors for class Quantity will be called (you guessed it)
Quantity
Constructors will differ depending on types.
One constructor of a class can call another.

Basic Quantity Constructor
To define a quantity, we need to give values to all three internal variables:

Getters
Attributes of objects should never be accessed within an object simply by referring to them:
Quantity x = new Quantity(É);
É
System.out.println(x.Num);
except possibly for debugging purposes.
Instead, use a getter method:
int getNum()
{
return Num;
}
...
System.out.println(x.getNum());

Reasons?
Static?
What is all this static?
In Java, a method may or may not depend on the object on which the method is called:
methods that do not depend on this object should be annotated as
static

Static can only call Static
A static method can only depend on
variables declared as static
other static methods
A static method, therefore, cannot depend on:
variables not declared as static
other methods not declared as static
The compiler will tell you, but maybe in a cryptic way.

Example
An Open List
Each list element begins a list in its own right.
A list is identified with a reference to its first element.
The empty list is identified with a special value.

Open Lists Identified with References
Sharing in Open Lists
Passing an Open List as an Argument to a Function
To pass an open list as an argument, we simply pass its reference.
The list is not literally copied.

Open List Consing
To ÒconsÓ an element to an open list, we simply put the element in a new cell and hook the cell to the original list:
consing x to the front [x | [a, b, c, d]] yields

Appending Open Lists
What happens when we append one open list to another, as in
append(L, M)?

Reversing an Open List
What happens when we reverse an open list?
reverse(L)

Mapping an Open List
What happens when we map over an open list?
L.map(fun)

OpenList Basics
OpenList(Object First, OpenList Rest)
L.first()
L.rest()
L.isEmpty()
cons(Object First, OpenList Rest)

Java Code for OpenList
public class OpenList
{
 private Object First;
 private OpenList Rest;
 // The unique empty list
 public static OpenList nil =
new OpenList(null, null);

Java Code for OpenList (2)
// Constructor

public OpenList(Object First, Object Rest)
   {
   ...
   }
// Get first element of a non-empty list.

public Object first()
   {
   ...
   }

Java Code for OpenList (3)
// Get rest of a non-empty list.

public OpenList rest()
   {
   ...
   }
// pseudo-constructor or ÒfactoryÓ for
// non-empty list

public OpenList cons(Object First)
   {
   ...
   }

Java Code for OpenList (4)
// emptiness test

public boolean isEmpty()
  {
  ...
  }

Static Methods
(closer to rex-style)
public static Object first(OpenList L)
public static OpenList rest(OpenList L)
public static boolean isEmpty(OpenList L)

Wrappers for Primitives
Items in a OpenList must be Objects.
Primitives (ints, longs, floats, doubles, chars É) are not Objects in Java.
The constructor Long( ) makes an Object for any long by creating a ÒwrapperÓ which is an object.
Other wrappers: Integer(), Float(), Double(), Boolean(), Snoop, Ice-T, ...

Strings
In contrast to long, int, float, etc. Strings are already objects.
Consequently, Strings do not need extra wrappers.
OpenLists are also Objects.

Getters for Wrappers
These can be applied to any Object derived from class Number, which includes Long, Integer, É:
longValue(), intValue(), É
Use the on-line javadoc pages on the web to find info:

http://java.sun.com/j2se/1.3/docs/api/

Conversion to String
Class String includes the following static methods (not constructors):

valueof(double d)
valueof(long x)
É
Each returns a String.

Cheap  Conversion to String
ÒAddingÓ a number to a string will convert the number to a string, then concatenate it:

String s = ÒÓ + 31415;

Conversion from String
Use the appropriate static method in the class to which you wish to convert, e.g.
Long.parseLong(String nm)
Double.parseDouble(String nm)
(DonÕt use getLong, which has a different meaning entirely.)

Type Discrimination
The type of an Object can be discriminated using the instanceof operator:

Object ob = L.first();

if( ob instanceof Long ) ...

if( ob instanceof OpenList ) ...

Equality Checking
To check whether two Objects are equal, DO NOT USE ==. This only checks whether the references to those objects are identical. The Objects could be equal, but be different Objects. This applies for Strings, for example.
DO USE equals:
if( ob1.equals(ob2) )

A Recursive List Pattern
(without using map)
ad-hoc map-like operations, build list outside-in, using recursion:

An Iterative List Pattern
build list inside-out, using ordinary iteration and an accumulator

An Iterative Reduce Pattern
collapse list  into a value using ordinary iteration

An Recursive Merge Pattern
merge two lists of Longs in increasing order

Try this
determine whether an Object occurs in a OpenList

If you used recursion, try it with iteration, and vice-versa
determine whether an Object occurs in a OpenList

Open vs. Closed Lists
Two list models are described in the text:
Open lists:
Elements and sublists can be shared
Mutation of lists is discouraged
Mathematically elegant
Closed lists:
Sharing generally not done
Mutation of lists is ok, because they are encapsulated
Mathematically less attractive
Closed lists can be built by wrapping open lists