Recitation 6

  1. Explain in your own words how fold functions exemplify the Abstraction Principle.

  2. Imagine that you are working in an object-oriented (OO) language that has a linked list class but that class does not implement fold_right—for example, the Java 8 LinkedList class. Implement fold_right. Since you can’t change the source code LinkedList, you’ll have to implement fold_right as a static method of a helper class. Make sure that your implementation does not modify the original list. We provide some Java starter code to help you.

Feel free to do this problem in a different OO language if you don’t know Java. But if you do know Java, please use it.

Hint: getting the type parameters correct is the trickiest part of this problem. Make sure you understand the OCaml types first, before trying to represent them in Java. Implementing Add is also a little tricky; you will probably want to create two classes that implement Func, not just one. If it’s not obvious why, then ponder the type of OCaml’s + operator very carefully.