On this page:
Intro
1 Lab Skeleton
2 Design Foldr
3 Use foldr for everything
4 Add filter
Submission
6.12

Lab 9: Fold!

Intro

Work in ad-hoc pairs. The two of you will work as a team to solve problems. At any time, one of you will be the Head and the other will be the Hands. The Head does the thinking and the Hands does the typing. Hands type only what the Head tells them to, but you’re free to discuss any issues that pop up. We’ll have you switch off during the lab to make sure each of you get practice problem solving, dealing with syntax, and getting finger exercises on the keyboard.

1 Lab Skeleton

You must start this lab with this project skeleton. Unzip the file into your IdeaProjects directory and open it with IntelliJ to get started.

It contains the code we wrote in class today.

2 Design Foldr

You’ve now seen how to implement map. Go for the gold and add foldr to your list interface.

You will need to define a new interface for representing binary functions, i.e. functions that consume two inputs.

If you need a reminder on what foldr does, see the ISL docs.

3 Use foldr for everything

Once you have foldr defined and it is working, define an abstract class for Lo<X>. For any exisiting method that can be defined in terms of foldr, such as map, come up with a definition of the method that uses foldr and place it in the abstract class. Be sure to test everything to make sure you haven’t introduced any bugs.

4 Add filter

Add a filter method that takes a predicate and produces a list of elements that satisfies the predicate. You can start by defining filter in the Empty and Cons class, but after you have a working, tested solution, try implementing filter in terms of foldr and putting it in the abstract class.

Submission

Submit a zip file of your work at the end of lab.