Homework 2

Due: 11:59pm, Thursday, Sep 15, 2022

100 points

For all Racket assignments in this course, set the DrRacket Language to Advanced Student (under How to Design Programs). Your assignment will be graded using the specified language level. If you use a different language level, your code may not work when it is graded.

For all Racket assignments in this course, set the DrRacket Language to Intermediate Student with lambda (under How to Design Programs). Your assignment will be graded using the specified language level. If you use a different language level, your code may not work when it is graded.

Do the following programming problems using only the primitives mentioned in Lectures 2 and 3.  Do not use the functions in the Racket library if they are not mentioned in Lectures 2 and 3.

  1. [30 pts] Section 14.2 in the HTDP First Edition book describes what it calls Binary Search Trees.  The terminology in this section of the book is non-standard because the Binary Search Trees contain both keys and values in each node and hence represents a finite mapping from keys to nodes.   The stub file contains describes a simple Racket programming problem (with a solution consisting of only a few lines of executable Racket code) based on essentially the same inductive data definition as Binary Search Trees but the type of the value field is parametric (alpha) which must be instantiated to symbol to match the explication of Binary Search Trees in the book. Your task is to 
    • Give some examples of the symbol-BSTM type.
    • Devise a set of test cases (input output pairs expressed using check-expect) for the getBSTM function.
    • Write a Template Instantiation for getBSTM (based on the general template for functions that process symbol-BSTMs)
    • Develop the code for the function getBSTM that satisfies the contract given in the stub file.
    • Briefly compare the asymptotic worst case running time of searching a symbol-BSTM that is well balanced (maximum depth is proportional to the log N where N is the number of keys in the symbol-BSTM) and function searches an ordered list of (key value) pairs represented as two element lists (as in Problem 2 below).

    Each of these five subtasks, except for devising the collection of test cases, takes only a few lines.  A good set of test cases might take as many as 10 lines.

  2. [30 pts] The stub file HW02.rkt provides a detailed description of how to develop the function cross (and supporting function cross-help) that consumes a number-list and a symbol-list and produces a number-symbol-pair-list where a  number-symbol-pair is represented by a two element list containing a number and a symbol.

  3. [30 pts] The stub file HW02.rkt provides a detailed description of how to develop the function merge (and supporting function merge-help) that consumes two ascending (technically non-descendingnumber-lists and merges them to form an ascending number-list.

  4. [10pts]  The ubiquitous Fibonacci function defined by the trivial fib program given in the stub tile HW02.rkt is interminably slow (exponential running time) for large inputs.  Develop a Racket function fastFib that consumes a natural number input n, produces the same answer as the fib function defined in the stub file, and runs in linear time (assuming that the primitive addition operation runs in constant time, which fails for very large n).  Hint: write a help function fastFibHelp that accumulates the result in an accumulator argument performing essentially the same computation as an imperative program relying on a loop that maintains fib(k-1) and fib(k-2) in mutable variables as k increases from 2 to n.  The poor efficiency the trivial functional program for fib is due to the fact that it repeatedly computes the Fibonacci function for small k exponentially many times.

    1. Show Type Contracts, Purpose Statements, Examples, and Template Instantiations for fastFibHelp and fastFib.  (The answers for the Template Instantiations can vary; only the salient features (primarily recursive calls) are matter.)
    2. As usual testing comes for free given that you provided input-output examples.  Make sure that after you run your program that no source code text (definitions of fastFib and fastFibHelp) is shaded in the DrRacket definitions panel.  Such shading indicates a failure to evaluate the shaded expressions in any test cases.