CS 131 Homework 2

Due by the beginning of class Wednesday, February 1
  1. Do Exercise 1.16 on page 26: write up, swapper, count-occurrences, flatten, and merge.

  2. Do part 1 of Exercise 1.17 on page 27: path. You may assume that n is somewhere in the tree. To make your code clearer, and to lessen the chance of going insane, first define and test three helper functions: root, left-child, and right-child, each of which takes a binary search tree (represented as a (nested) list) and returns the appropriate subcomponent. Then write path in terms of these functions instead of using car and cdr directly.

  3. Do part 1 of Exercise 1.18 on page 27: compose.

  4. Write the function (compose-all funcs), which takes a list of functions and returns a new function representing their composition. Your compose-all function should use compose as a helper function. Examples:

    ((compose-all (list car cdr cdr cdr)) '(a b c d e)) => d
    ((compose-all (list sqrt abs car)) '(-36 49 2)) => 6
    ((compose-all '()) '(a b c d)) => (a b c d)
    
    The list function is a built-in Scheme primitive that takes any number of expressions as arguments and returns a list of their corresponding values.

Extra credit

You should finish the other problems first before attempting these.

  1. Do part 2 of Exercise 1.18 on page 27: car&cdr.

  2. Do part 3 of Exercise 1.18 on page 27: car&cdr2.

Turning in your homework

Put all of your function definitions into a single Scheme file called assign2.ss and submit this file electronically by running the following script from any Pomona Linux machine:

/common/cs/submit/cs131-submit

You must run this script from the directory that contains your Scheme file. You will be prompted for the assignment name (in this case, hw02) and then the name of your file. In general, cs131-submit will allow you to submit any number of files, one at a time, but for this assignment you only need to submit one file. More information about cs131-submit is available here.

For convenience, you might want to add an alias to your .bashrc file as follows:

alias cs131-submit='/common/cs/submit/cs131-submit'

If you have questions about anything, don't hesitate to ask!