Versions Compared

Key

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

...

Since Object is the superclass of all concrete classes in Java, in practice, we often have to perform type-casting on the arg in order to perform the desired operation. More than often, we need to type-cast the returned result in order to make proper use of it.

Finger exercises:

HTML
<ol>
HTML
<li>

Use DrJava to compile the above UnaryFun interface.

HTML
</li>

...


Now create and compile a new file containing the following class.

Code Block
    class Lab8Exercises {
        static Lab8Exercises ONLY = new Lab8Exercises ();
        private Lab8Exercises () {}
    }
HTML
</li>
HTML
<li>

Add the following method to FingerExercises.

...

(Yes, the code is hard to read because of the “ugly” "ugly" syntax.) What does it do? Use the Interactions pane to test it out.

...

...

</li>
HTML
<li>

Now remove the type cast (Integer) on arg in the above code. Does it compile? Do you see why the type cast is necessary? OK, put back the type cast so that everything compile

HTML
</li>
HTML
<li>

Add the following method.

...

What does it do?
Now remove the first Integer type cast that immediately follows the return in the above code. Does it compile? Put pack the type cast so that everything compiles.

...

HTML
<li>

The method in exercise 5 is an example of what is called “Curry"Curry-ing”ing"( in honor of the mathematician Curry) where a function of two variables x, y is expressed a function of x whose value is a function of y. Any function of two variables, f (x, y), can be represented as a combination of two functions, each of which is a function of one variable. For example,

  • suppose f(x, y) = x + y;
  • let F be a function of one variable x, such that F(x) (error) is a function G of one variable y, where G(y) (thumbs up) {{ f(x, y) }} x + y;
  • then F(x) (y) (error) (thumbs up) {{ x + y }} f(x, y);

In the same manner, any function of three variables can be represented as a combination of three functions, each of which is a function of one variable via currying.

...

Code Block
UnaryFun whatIsIt() {
   return new UnaryFun() {
        Object apply(Object s1) {
            return new UnaryFun() {
                Object apply(Object s2) {
                    return new UnaryFun() {
                        Object apply(Object s3) {
                            return s1 + " " + s2 + " " + s3;
                        }
                    };
                }
            };
        }
    };
}   

Actually the above does not compile in the Intermediate language level. In full Java syntax, the code would like the following (and would compile):

Code Block
public class Currying {
    UnaryFun whatIsIt() {
       return new UnaryFun() {
            public Object apply(final Object s1) {
                return new UnaryFun() {
                    public Object apply(final Object s2) {
                        return new UnaryFun() {
                            public Object apply(final Object s3) {
                                return s1 + " " + s2 + " " + s3;
                            }
                        };
                    }
                };
            }
        };
    }   
}
HTML
</li>
HTML
</ol>

IMPORTANT CONCEPT: CLOSURE

...

Code Block
abstract class ObjectList {
  ObjectList cons(Object n) { return new ConsObjectList(n, this); }
 
  /** Applies f to each element in this list and returns the list containing
    * the results of this application. */
  abstract ObjectList map(UnaryFun f);
 
  /** Returns a String representation of this list as specified in each concrete subclass. */
  abstract String listString();

  /** Returns s String containing the elements of this, where each element is preceded by ' ' */
  abstract String listStringHelp();
}

class EmptyObjectList extends ObjectList {
  static EmptyObjectList ONLY = new EmptyObjectList();
  private EmptyObjectList() { }

  ObjectList map(UnaryFun f) { return null; /*to do */ } 
  String listString() { return "()"; }
  String listStringHelp() { return ""; }
}

 

class ConsObjectList extends ObjectList {
  Object first;
  ObjectList rest; 

  ObjectList map(UnaryFun f) { return null; /* ... rest.map(f) ...to do*/ } 

  String listString() { return "(" + first + rest.listStringHelp() + ")"; }
  String listStringHelp() { return " " + first + rest.listStringHelp(); }
}
HTML
<ol start="7">
HTML
<li>

Implement the map method for ObjectList .

...


...

</li>
HTML
<li>

Add and implement the following method for class Lab8Exercises.

Code Block
    /**
     * Assuming the list L contains integers, returns the list of booleans
     * whose n-th element is true if the number at the n-th element in L is
     * positive, false otherwise.
     */
    ObjectList checkPositive(ObjectList L) {
        return null; // to do
    }
HTML
</li>
HTML
<li>

Add and implement the following method for class Lab8Exercises.

Code Block
    /**
     * Assuming the list L contains integers, returns the list whose n-th
     * element is the square of the n-th element L.
     */
    ObjectList squareList(ObjectList L) {
        return null; // to do
    }
HTML
</li>
HTML
<li>

Add and implement the following method for class Lab8Exercises.

Code Block
    /**
     * Assuming the list L contains integers, returns the list of integers
     * whose n-th element is the lesser of the n-th element of L and upperBound
     */
    Object cutOff(ObjectList L, int upperBound) {
        return null; // to do
    }
HTML
</li>
HTML
<li>

Create and compile a file containing the following class

...

Now write the lookup method for ObjectList .

HTML
</li>

...

Add and implement the following method for class Lab8Exercises.

Code Block
    /**
     * Assuming the list dict contains Pairs, returns the list of Objects whose
     * n-th element is the Pair in dict whose key is the n-th element of the
     * list L.
     */
    ObjectList translate(ObjectList L, ObjectList dict) {
        return null; // to do
    }
HTML
</li>
HTML
</ol>

Access Permissions: (Please don't edit)

...