Homework #2

Due: Tues, Jan 19

How to Submit

  1. Assign yourself to a “Group” in Canvas. Select “People” from the menu, then select the “Groups” tab. Click “Join” on a group whose name starts with “HW2”.

    • You must coordinate with your team members to all join the same group. When choosing a group, please use the smallest available group number.

    • Do not add yourself to a group unless you have already coordinated and everyone has agreed to be in a group together.

  2. Upload a single file named HW2.hs for your team for this assignment in Canvas. Only one team member needs to upload the file.

    • Your file must contain the names and ID numbers of each member of your group in a comment at the top of your file. This is to ensure that nobody changed the composition of your group.

    • Your file must compile without errors in GHCi. If some of your code does not work, comment it out. If your code does not compile, the TAs will evaluate it harshly!

    • Do not change the existing doctest comments in the template (the lines starting with >>> and the results underneath). However, you are free to add your own doctest comments in addition to the ones that are there. The TAs will grade using both doctest and manual inspection of your file. If needed, they may also load your file in GHCi to run additional tests.

    • If you can’t solve a problem, you can get partial credit by describing in comments what you tried and where you got stuck. Often times, writing this out helps you get unstuck!

Description

The goal of this assignment is to further develop your functional programming skills. In particular, to get more practice working with data types and defining functions using pattern matching and recursion.

In this assignment, you’ll again be working with binary trees. However, the data type you’re using in this assignment is different from the tree data type from Homework 1 in two ways: First, the type of value stored in each node is now parameterized rather than fixed to Int. This means that you can have trees of integers, characters, boolean values, functions, or any other Haskell type. Second, the unary Leaf constructor has been replaced by a nullary End constructor. This is more flexible since it doesn’t require every node in a tree to have either zero or two children.

Download this template (HW2.template.hs), rename it to HW2.hs, and enter your solutions directly in the file.

For each undefined function in the template, you should:

  1. Write the appropriate type of the function.
  2. Implement the function.

As before, the comment preceding each function gives a brief English description of what the function should do. Additionally, each comment contains doctest examples that illustrate the intended behavior of the function. You can use these examples both to help understand the intended behavior of the function, and as unit tests.

I’ve given the type of the last function you must implement since it involves a type class constraint. The constraint Eq a => means that the function will work for all trees that contain values that can be checked for equality (like integers, characters, and booleans).

Besides Haskell lists and the types defined in the template, you’ll also need Haskell’s Maybe data type. We covered this briefly in class, but if you need a refresher, see Chapter 3 of Brent Yorgey’s Intro to Haskell.


Back to course home page