Assignment 2, CS 152
due October 8, 2003

Construct a file a2.scm that contains definitions for the following Scheme functions in Scheme. Functions 1-4 are worth 15 points each. Test your definitions with the code in the file A2tester.scm, which is available on the class web site.

  1. Define a function add-number-to-symbol that takes a symbol and an integer, and returns a new symbol consisting of the input symbol followed by the integer. In case of illegal arguments, return the first argument unchanged.

  2. Define a function filter-and-apply that takes a list, a predicate on list elements, and a unary processing function on list elements, and returns the list obtained by removing all elements from the list that fail to satisfy the predicate, and replacing all other elements by the value when the function is applied to them. You needn't worry about error handling.

  3. Define a function number-of-binary-trees that takes a nonnegative integer n and returns the number of (nonisomorphic) binary trees with n nodes. The function should return an error message (as a string) if n is not a nonnegative integer. Note that for n nodes, the left subtree may have anywhere from k=0 to k=n-1 nodes, and the right subtree must have n-1-k nodes. So for n=0, 1, 2, 3, and 4, the appropriate return values are 1, 1, 2, 5, and 14.

  4. Define a function append-all-pairs that takes two lists of lists, and returns a list of those lists that can be obtained by appending an element of the first list to an element of the second list. So if the input lists have length m and n, the output list will have length mn. You needn't check for illegal input.

  5. Using define-struct, define a type 3D-point for representing points in 3-dimensional space. Also define the four functions below. All of the functions should return #f in case of illegal input.