CS 307: 7. Treasure Hunt

Due: Friday, October 19, 2001

  1. Write functions that will find the sum, mean (average), mean square, variance, and standard deviation of a list of numbers. The variance is equal to the mean square minus the square of the mean. The standard deviation is the square root of the variance. Calculate these values for the following list of numbers:

       (95 72 86 70 97 72 52 88 77 94 91 79 61 77 99 70 91)
    

  2. Binomial coefficients are the numeric factors of the products in a power of a binomial such as x + y. For example, (x + y)^2 = x^2 + 2 x y + y^2 has the coefficients 1 2 1. Binomial coefficients can be calculated using Pascal's triangle:
                1
             1     1
          1     2     1
       1     3     3     1
    1     4     6     4     1
    

    Each new level of the triangle has 1's on the ends; the interior numbers are the sums of the two numbers above them. Write a program (binomial n) to produce a list of binomial coefficients for the power n using the Pascal's triangle technique. (binomial should be a recursive program that manipulates lists; it should not use use (choose n k).) Use the function (choose n k) that you wrote earlier to calculate (choose 4 k) for k from 0 through 4; what is the relationship between these values and the binomial coefficients?

  3. Write a program (interpolate l x) that will make a list of numbers appear to be a continuous function by linearly interpolating for values of x that are not integers. Assume that the first element of the list is the value for x = 0, the second element is the value for x = 1, etc. For cases where i <= x < i+1, let delta = (x - i) and compute the value f(x) = l(i) + delta * (l(i+1) - l(i)) , where l(i) denotes the ith element of the list l. The output of interpolate should be a single number.

    For example, suppose that l = (0 30 56 78 96 ...). The value for x = 3 is 78, and the value for x = 4 is 96. (interpolate l 3.4) = 85.2, i.e., 78 + .4 * (96 - 78) .

    Use your interpolate function to make the binary coefficient list for n = 12 look like a function and plot it using your plotting program. What is the shape of this curve?

  4. Write a function (count item tree) that counts occurrences of item in the list structure tree. Use eqv? to test.

  5. You have been assigned to explore a cave to see whether it contains a treasure. The treasure is too heavy for you to carry out; you must return instructions to get to the treasure. Each room of the cave is either a junction, with two connecting passages called car and cdr, or it is a dead end that may or may not be a treasure.

    1. Write a function (findpath item cave) that will find a path to a part of cave that matches item; use eqv? to test for equivalence. findpath returns #f if item does not occur in cave; otherwise, it returns a list of car's and cdr's that describes the path to the item. findpath is easily written as a recursive function. Examples:
      (findpath 'a 'b)     = #f
      (findpath 'a 'a)     = ()
      (findpath 'a '(a))   = (car)
      (findpath 'gold '(rocks gold (monster))) = (cdr car)
      

    2. Write an interpreter (follow path cave) that will follow a path as returned by findpath and retrieve the contents of cave at the location specified by path.

    3. Write a function pathtocode that will convert a list of steps produced by findpath into Lisp code to extract the item from a variable x that points to the top of the tree. Hint: look at the reverse function.
      (pathtocode (findpath 'gold '(rocks gold (monster))) = (car (cdr x))
      

    4. Write a function (makedefine fn vars code) that makes a define form for a function named fn whose arguments are vars (a list of variable names) and whose code is code.

    5. A robot is to be sent into the cave to retrieve the treasure; naturally, the robot is programmed in Scheme. Write a function (pathfn fn item cave) that will create and define a function named fn to get the specified item from the cave. Use eval to execute the define form produced by makedefine. Test the function to verify that it does extract the item from the cave. Example:
      (define cave '(a (b ((c) d) (e (f)) g) h . i))
      (pathfn 'get-f 'f cave)
        => (define (get-f x) (car (car (cdr (car (cdr (cdr (car (cdr x)))))))))
      (get-f cave)  =  f
      
      Verify that your program pathfn can make a function to retrieve any of the letters in cave. You now have written a Scheme program that writes Scheme programs!
    6. Write a recursive function (corresp item tree1 tree2) that finds the item, corresponding to item in tree1, in tree2. Example:
      (corresp 'light '((my eyes) (have seen (the light)))
                      '((my ears) (have heard (the music))))
        ==> music
      
    7. Write a second version of (corresp item tree1 tree2) using pathfn.