CS 314 Assignment 3: More Recursion and Lists

Due: September 28, 2017.

Files: Cons.java   test3.lsp

This assignment may be done in Java or in Lisp.

  1. Write functions that find the sum, mean or average (mean = (sum xi) / n), meansq (mean square = (sum xi2) / n), variance, and stddev (standard deviation) of a list of Integer. sum should be int, while the others should be double. The variance is equal to the mean square minus the square of the mean (= mean2). 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. The sine function can be computed from its Taylor series expansion:
              1    3    5    7    9
             x    x    x    x    x
    sin(x) = -- - -- + -- - -- + -- - ...
             1!   3!   5!   7!   9!
    
    Write a function double sine(double x) to compute sine using this series. You may not use the functions Math.pow or factorial; instead, write a tail-recursive auxiliary function to compute each term of the series from the previous term and add it to the sum. Stop after the 21st power of x. Hint: write down how each term of the series differs from the previous term.

  3. Write a recursive function nthcdr(int n, Cons lst) that returns lst after applying rest to it n times. If there are fewer than n items in lst, return null.

  4. Write a function elt(Cons lst, int n) that retrieves the nth item in the list, the first item being numbered 0. This allows a list to be used like an array; how does its Big O compare with array access?

  5. Write a function double interpolate(Cons lst, double x) that will make lst, a list of Integer, appear to be a continuous function by linearly interpolating for values of x. 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) = lst(i) + delta * (lst(i+1) - lst(i)) , where lst(i) denotes the ith element of the list lst.

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

    Use your interpolate function to make the binomial coefficient list for n = 12 look like a function. What is the shape of this curve?

    http://en.wikipedia.org/wiki/Interpolation

  6. Write a recursive function sumtr(Cons lst) that adds all the numbers in lst. The list lst may contain not only Integer, but also sublists, recursively. Your function should sum the values in the sublists also. You may assume that the only things in a list are Integer and sublists; you can recognize a sublist because consp will be true.
       (sumtr '(1 (2 3) ((4 5) 6)))  =>  21
    
  7. Write a function subseq(Cons lst, int start, int end) that returns a new list that is a sub-sequence of lst beginning with the item numbered start and including items through end - 1. We will assume that the input lst contains enough elements.
       (subseq '(0 1 2 3 4 5 6)  2  5)  =>  (2 3 4)
    
  8. A filter is a function that allows desired elements to pass through, while removing undesired elements. Write a recursive function posfilter(Cons lst) that returns a new list containing only the non-negative values in lst, a list of Integer, in the same order as in the original list.
       (posfilter '(3 17 -2 0 -3 4 -5 12))  =>  (3 17 0 4 12)
    
  9. A predicate is a boolean function, which tests its argument and returns true or false. Write a recursive function subset(Predicate p, Cons lst) that returns a new list containing only the values in lst that satisfy the predicate p. In mathematical notation, this would be written { x ∈ lst | p(x) }, pronounced the set of x in lst such that p(x). We will assume that p.pred(item) can be used to test an item. The output list should be in the same order as the original list.

    If we assume that the predicate myp tests for integers > 3, then:

       (subset 'myp '(3 17 -2 0 -3 4 -5 12))  =>  (17 4 12)
    
  10. A mapping relates an element of a set, called its domain, to an element of another set, called its range; we write M: D → R. Write a recursive function mapcar(Functor f, Cons lst) that returns a new list containing the results of applying f.fn(item) to each item in the list lst. The output list should be in the same order as the original list.

    If we assume that the functor myf adds 2 to an Integer,

       (mapcar 'myf '(0 1 2 3 4 5 6))  =>  (2 3 4 5 6 7 8)
    
  11. Write a recursive function Object some(Predicate p, Cons lst) that returns the first item in lst that satisfies the predicate p. In mathematical notation, this would be written ∃ x   p(x), and pronounced there exists an x such that p(x) or for some x, p(x).
       (some 'myp '(3 17 -2 0 -3 4 -5 12))  =>  17
    

  12. Write a recursive function boolean every(Predicate p, Cons lst) that tests whether every item in lst satisfies the predicate p. In mathematical notation, this would be written ∀ x   p(x), and pronounced for all x, p(x).
       (every 'myp '(3 17 -2 0 -3 4 -5 12))  =>  false