Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Homework 1

Due: 11:59pm, Thursday, Sep 0809, 20222024

100 points

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.

Carefully follow the Sample Solution to a Programming Problem in the Racket HW Guide. Only half of the credit for each programming problem is based on the the correctness of your code as determined by our test cases. Much of the grade is based on how well you follow the design recipe.  For a crisp example of what an ideal solution looks like look at the Sample Solution to a Programming Problem that sorts list-of-number at the end of Sample Solution to a Programming Problem in the the Racket HW Guide.  This process may appear painful but it shows "in the small" how program design should be done "in the large".   It is not difficult.  If you carefully inspect the sample program, it shows the level of detail in describing your program design (and its derivation!) that we want.

  1. 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.

  2. [10 pts] Develop the function contains? thatconsumes a symbol and a list of symbols and determines whether or not the symbol occurs in the list.

  3. [10 ptsDevelop the function count-symbolsthat consumes a list of symbols and produces the number of items in the list.

  4. [10 ptsDevelop the function count-numbers that counts how many numbers are in a list of numbers.  [Note: the function merely works on inputs that are lists of numbers; it may blow up on anything else].

  5. [20 ptsDevelop the function avg-price. It consumes a list of toy prices and computes the average price of a toy. The average is the total of all prices divided by the number of toys. Toy prices are numbers.
     
    Hints:
    1. Develop a few auxiliary functions (following the design recipe for each auxiliary function) to make the definition of avg-price
 very
    1. very easy.  Do not use the Racket library other than primitives mentioned in class or lecture slides.  If in doubt, ask the instructor.
    2. If the list of toy prices is empty, the function avg-price produces an error message as described in Guidance below. 

  1. [10pts] Develop the function elim-exp to eliminate expensive toys from a price list. The function consumes a number, called mp (short for "maximum price") and a list of toy prices, called lotp, and produces a list of all those prices in lotp that are below or equal to mp.  For example,
        (check-expect
  1. (elim-exp
  1. 1.0
  1. (list
  1. 2.95
  1. .95
  1. 1.0
  1. 5))
  1. (list
  1. .95
  1. 1.0))
  1. =
  1. #true
  2. [10pts] Develop the function delete to eliminate specific toys from a list. The function consumes the name of a toy, called ty, and a list of names, called lon, and produces a list of names that contains all components of lon with the exception of ty. For example,
        (check-expect
  1. (delete
  1. 'robot
  1. (list
  1. 'robot
  1. 'doll
  1. 'dress))
  1. (list
  1. 'doll
  1. 'dress))
  1. =
  1. #true

  2. [30pts] A list can be used to represent a finite set. For example,
  
  1.   (list
  1. 'c
  1. 'o
  1. 'm
  1. 'p)
  1. represents
  1. the
  1. set
  1. of
  1. symbols
  1. {'c
  1. ,
  1. 'o,
  1. 'm,
  1. 'p}.  In
  1. such
  1. a
  1. representation,
  1. we
  1. assume
  1. all
  1. elements
  1. in
  1. the
  1. list
  1. are
  1. unique;
  1. there
  1. are
  1. no
  1. duplicates.   Develop
the function power that consumes a list of symbols los (representing a set) and produces a list of list of symbols representing the power set (set of all subsets) of los.  Hint: write an auxiliary function cons-all that consumes a symbol sym and a list of list of symbols lolos and inserts symbol sym at the front of each list in lolos. 

For example, 

  1. the function power that consumes a list of symbols los (representing a set) and produces a list of list of symbols representing the power set (set of all subsets) of los.  Hint: write an auxiliary function cons-all that consumes a symbol sym and a list of list of symbols lolos and inserts symbol sym at the front of each list in lolos. For example, 
   

(check-expect (cons-all 'a (list (list 'c) (list 'o) (list 'm) (list 'p))  (list (list 'a 'c) (list 'a 'o) (list 'a 'm) (list 'a 'p))) = #true


  • Guidance:

    1. Follow the design recipe imitating Sample Solution to a Programming Problem in the Racket HW Guide.

    2. Problem 4 asks you to write a function that checks for the empty list as an input error and throws an aborting error in the case. (The purpose statement should document this behavior!) In DrRacket, the error function takes a single argument (not two arguments as documented in the book) and returns an error object containing the passed argument (typically a string). We recommend using a string (text enclosed in double quotation marks) like "An empty list of toy prices triggers this aborting error" as the argument.  You can test the error-throwing behavior of a function using check-error which is documented in the DrRacket Help Desk.

    3. Study Figure 26 in 9.4 for a detailed description of the design recipe and how it is documented in the program text that you develop.

      To follow the design recipe, you must write down the type signature and purpose, provide at least 3 well-chosen examples (more for complex functions like power), write the template for the function (which is trivial when no recursion is involved), write the code for the function, and  confirm that the previous examples are sufficient to test the function  function (adding more examples if necessary). You should bundle the examples and test cases together as a block of check-expect invocations, which was not part of DrRacket when the First Edition of the book was written. These tests should precede your follow the signature and purpose statement and precede the function template and code.  ( DrRacket does not evaluate top-level calls on check-expect until the all other top-level code has been executed.)

    4. Your chosen examples should illustrate the output you expect, and the test cases should produce the actual output. You can write these test cases as executable code using the check-expect primitive which exists in the DrRacket teaching languages.  If the function processes an inductive type, make sure that your examples include the base case(s) and sample inductive cases.

    5. Question 8 is clearly the most challenging problem in the assignment.  Tackle writing the auxiliary function cons-all first; this task is similar to answering earlier questions.  Then work through (via manual evaluation) a few very simple examples--most notably the empty list (set)--using your function template and what code you think goes in the ellipsis zones of the template.  How many subsets does the empty set have?  Recall that the empty set is a set.

 

Write a comment…