CS 1101 - A-term 10

Homework 10 - Higher-Order Functions

Due: Tuesday, October 5 at 5pm


Assignment Goals

  • To make sure you can write programs using higher-order functions, like map and filter.
  • To get more practice using the data definitions and templates for binary trees.

    Remember to follow the Expectations on Homework when preparing your solutions.


    The Assignment

    In Homeworks 5 and 6 we developed programs for lists of structs using the information on lists of borrowers for a micro-lending institution like Kiva. In this homework you'll start by rewriting a couple of the Kiva exercises, using higher-order functions like map and filter. Then you'll develop a set of higher-order functions that can be used to iterate over trees.

    Start with these data definitions from Homework 5 and 6:

    ;; a borrower is a (make-borrower string number string string number number)
    (define-struct borrower (name team-size country business requested percent-raised))
    
    ;; a list-of-borrower is either
    ;; empty or
    ;; (cons borrower list-of-borrower)
    
    Here is a set of solutions to Homework 6 that you may refer to as you work on Problems 1 and 2.
    1. In Homework 6 you wrote a function find-by-country, with the following contract and purpose:
      ;; find-by-country: string list-of-borrower -> list-of-borrower
      ;; consumes the name of a country and a list of borrowers and produces a 
      ;; list of only those borrowers from the given country
      
      Write a new version of find-by-country that uses filter.

    2. In addition to map and filter, DrRacket provides a number of other abstract functions for processing lists. One of these is called foldl. foldl iterates a function over the items in a list to build up a return value. For example, you can use foldl to sum the numbers in a list of numbers like this:
      (foldl + 0 (list 3 4 5))
      
      foldl consumes a function, an initial value, and a list. In the given example, the function being applied to the items in the list is +, the initial value is 0, and the list to be summed is (list 3 4 5 ). The return value in this case is 12.

      Use foldl and map to rewrite another function from Homework 6, funds-needed. Here's the contract and purpose:

      ;; funds-needed:  list-of-borrower -> number
      ;; consumes a list of borrowers and produces the sum of all amounts still needed
      
      (You may also want to make use of the helper from Homework 6, funds-of-one.)
    For the remaining exercises, we'll work with the following data definition for binary trees:
       ;; A bintree[alpha] is either
       ;;  - false, or
       ;;  - a node
    
       ;; a node is a (make-node alpha bintree bintree)
       (define-struct node (item left right))
    

    1. Write the template for programs over bintrees. You can check for false using boolean?

    2. Write a program sign-tree that consumes a bintree[number] (i.e., a bintree where the items at each node are numbers) and returns a bintree[symbol] with the same structure as the input tree, but with each number replaced with one of the symbols 'positive, 'negative, or 'zero, as appropriate.

    3. There is a struct called a posn that's built-in to DrRacket. A posn can be used to represent the x and y coordinates of a point. Here is the data definition for a posn:
      ;; a posn is a struct
      ;; (make-posn number number)
      (define-struct posn(x y))
      
      Write a program flip-tree that consumes a bintree[posn] and returns a bintree[posn]. The returned tree should have the same structure as the input tree, but each posn should have its x- and y-coordinates flipped [i.e., (make-posn 3 4) would become (make-posn 4 3)].

    4. The programs sign-tree and flip-tree have a similar structure. Write a function tree-map that takes a bintree and a function and applies the given function to each node in the tree. Show how to implement sign-tree and flip-tree using tree-map. Be sure to include a correct contract for tree-map.

    5. Write a program tree-andmap that consumes a function of type (alpha -> boolean) and a bintree[alpha] and returns a boolean indicating whether the given function returned true at every node. Using tree-andmap, implement pos-tree? that consumes a bintree[number] and determines whether all numbers in the tree are positive.

    What to Turn In

    Here is the HW 10 grade sheet that the TAs will use when grading your homework.

    Using web-based turnin, turn in a single file containing all code and documentation for this assignment. Follow the naming conventions when naming your file. Please make sure both partners' names and wpi login names are listed in a comment at the top of the file.