EXERCISE SET #4

1. Define a Scheme predicate that tests whether a given list is palindromic -- whether its first element is equal? to its last, its second to its next-to-last, and so on.

2. Define a Scheme procedure display-structure that uses display to print out a representation of any symbolic structure, as defined in exercise set #3. It should use the following representation conventions:

a. The null object should be displayed as #n.

b. A symbol should be displayed as itself; for instance, the symbol alpha should be displayed as alpha.

c. A pair should be displayed as a left parenthesis, the displayed version of its left field, a space, a dot, another space, the displayed version of its right field, and a right parenthesis. Use recursive calls to display-structure to print the left and right fields.

I don't care what value display-structure returns. #<unspecified> would be fine.

3. Define a Scheme procedure that returns the number of pair-records that make up a given symbolic structure. (In other words, in the box-and-arrows diagram for the structure, how many of those domino-shaped boxes would there be?)

4. Define a Scheme procedure that constructs and returns the mirror image of a given symbolic structure -- the symbolic structure that contains the same constituents, but with all cars and cdrs swapped.

5. Repeat exercise 4 of exercise set #3, but this time define a procedure that obtains the result by an iterative process.

6. Repeat exercise 1 of exercise set #3, using let-expressions to simplify the code.

7. Repeat exercise 10 of exercise set #3, using a letrec-expression to define the helper procedure locally.

8. Define a Scheme procedure display-in-binary that writes out, as a side effect, the binary numeral for a given natural number. For instance, the call (display-in-binary 42) should cause the numeral 101010 to be written out. I don't care what value display-in-binary returns.

9. Define a Scheme procedure filter that takes two arguments, a predicate and a list, and returns a list that contains just the elements of the given list that satisfy the given predicate (that is, the elements for which applying the predicate to the element would return #t). For instance, (filter odd? '(3 8 6 1 4 9 2)) should return (3 1 9).

10. The Catalan sequence is defined by the following recursion:

  • C(0) is 1.

  • For any positive integer n, C(n) is (C(0) * C(n - 1)) + (C(1) * C(n - 2)) + ... + (C(k) * C(n - k - 1)) + ... + (C(n - 2) * C(1)) + (C(n - 1) * C(0)).
  • In other words, to obtain C(n), find all the ways of multiplying together two earlier members of the Catalan sequence whose subscripts add up to n - 1 and add up all the products.

    Here are some values of the sequence:

    C(0) = 1
    C(1) = 1
    C(2) = 2
    C(3) = 5
    C(4) = 14
    C(5) = 42
    
    Define a Scheme procedure that takes n as an argument and computes C(n).


    created June 12, 1996
    last revised June 12, 1996

    John David Stone (stone@math.grin.edu)