/** ListVistor that implements the scalarProduct "method" on List. */ class ScalarProductVisitor implements ListVisitor { /** other is field storing the "argument" to the scalarProduct "method"; assumed equal in length to the "host". */ private List other; ScalarProductVisitor(List o) { other = o; } public Integer forEmptyList(EmptyList el) { return 0; } public Integer forConsList(ConsList cil) { ConsList otherCons = (ConsList) other; // cast other to a ConsList return (cil.first)*(otherCons.first()) + cil.rest().visit(new ScalarProductVisitor(otherCons.rest())); } }