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.
- 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.
- 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.
- 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.
- 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.
- 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.
-
build-3D-point, which takes three real numbers and returns a 3D-point representation of a point in 3-dimensional space, with the given numbers as coordinates (in the given order),
-
equal-3D-points?, which takes two 3D-points and deterimines whether they represent the same point,
-
3D-point-printname, which takes a 3D-point and returns a string representing its print name (where the coordinates
are separated by commas and enclosed in parentheses), and
-
3D-distance, which returns the distance between two points in 3-dimensional space, where the points are represented using the 3D-point type.