Do the following sets of problems from HtDP:
Functions: 2.3.3, 3.1.1, 3.1.2, 3.1.3, and 3.3.2, 3.3.3, 3.3.4
An old-style movie theater has a simple profit function. Each customer pays $5 per ticket. Every performance costs the theater $20, plus $.50 per attendee. Develop the function
total-profit
. It consumes the number
of attendees (of a show) and produces how much income the attendees
produce. Solution
The next step is to make up examples for each of the functions. Determine how many attendees can afford a show at a ticket price of $3.00, $4.00, and $5.00. Use the examples to formulate a general rule that shows how to compute the number of attendees from the ticket price. Make up more examples if needed. Solution
Use the results of exercise 1 to determine how much it costs to run a show at $3.00, $4.00, and $5.00. Also determine how much revenue each show produces at those prices. Finally, figure out how much profit the monopolistic movie owner can make with each show. Which is the best price (of these three) for maximizing the profit? Solution
Determine the profit that the movie owner makes at $3.00, $4.00, and $5.00 using the program definitions in both columns. Make sure that the results are the same as those predicted in exercise 1. Solution
Develop the program
volume-cylinder
. It consumes the radius of a
cylinder's base disk and its height; it computes the volume of the
cylinder. SolutionDevelop
area-cylinder
. The program consumes the radius of the
cylinder's base disk and its height. Its result is the surface area of the
cylinder. SolutionDevelop the function
area-pipe
. It computes the surface area of
a pipe, which is an open cylinder. The program consumes three values: the
pipe's inner radius, its length, and the thickness of its wall.Develop two versions: a program that consists of a single definition and a program that consists of several function definitions. Which one evokes more confidence? Solution
Structures: 6.3.1, 6.3.2, 6.4.1, 6.5.1
Consider the following structure definitions:
(define-struct movie (title producer))
(define-struct boyfriend (name hair eyes phone))
(define-struct cheerleader (name number))
(define-struct CD (artist title price))
(define-struct sweater (material size producer))
What are the names of the constructors and the selectors that each of them adds to Scheme? Draw box representations for each of these structures. Solution
Consider the following structure definition
(define-struct movie (title producer))
and evaluate the following expressions:
(movie-title (make-movie 'ThePhantomMenace 'Lucas))
(movie-producer (make-movie 'TheEmpireStrikesBack 'Lucas))
Now evaluate the following expressions, assuming x
and y
stand for arbitrary symbols:
(movie-title (make-movie x y))
(movie-producer (make-movie x y))
Formulate equations that state general laws concerning the relationships of
movie-title
and movie-producer
and
make-movie
. Solution
Provide data definitions for the following structure definitions:
(define-struct movie (title producer))
(define-struct boyfriend (name hair eyes phone))
(define-struct cheerleader (name number))
(define-struct CD (artist title price))
(define-struct sweater (material size producer))
Make appropriate assumptions about what data goes with which field. Solution
Develop templates for functions that consume the following structures:
(define-struct movie (title producer))
(define-struct boyfriend (name hair eyes phone))
(define-struct cheerleader (name number))
(define-struct CD (artist title price))
(define-struct sweater (material size producer))
. Solution
Unions: 7.2.1
Develop structure and data definitions for a collection of zoo animals. The collection includes
Then develop a template for functions that consume zoo animals.
Develop the function fits?
. The function consumes a zoo animal and
the volume of a cage. It determines whether the cage is large enough for
the animal. Solution
: Lists: 9.3.3, 9.5.1, 9.5.2, 9.5.3, 9.5.4
Develop the function
contains?
, which consumes a symbol and a list
of symbols and determines whether or not the symbol occurs in the
list. Solution
Use DrScheme to test the definition of
sum
on the following sample
lists of numbers:
empty (cons 1.00 empty) (cons 17.05 (cons 1.22 (cons 2.59 empty)))
Compare the results with our specifications. Then apply sum
to the
following examples:
empty (cons 2.59 empty) (cons 1.22 (cons 2.59 empty))
First determine what the result should be; then use DrScheme to evaluate the expressions. Solution
Develop the function
how-many-symbols
, which consumes a list of
symbols and produces the number of items in the list.
Develop the function how-many-numbers
, which counts how many
numbers are in a list of numbers. How do how-many-symbols
and
how-many-numbers
differ? Solution
Develop the function
dollar-store?
, which consumes a list of prices
(numbers) and checks whether all of the prices are below 1
.
For example, the following expressions should evaluate to true
:
(dollar-store? empty)
(not (dollar-store? (cons .75 (cons 1.95 (cons .25 empty)))))
(dollar-store? (cons .75 (cons .95 (cons .25 empty))))
Generalize the function so that it consumes a list of prices (numbers) and a threshold price (number) and checks that all prices in the list are below the threshold. Solution
Develop the function
check-range1
, which consumes a list of
temperature measurements and checks whether all measurements are between
and .
Generalize the function to check-range
, which consumes a list of
temperature measurements and a legal interval and checks whether all
measurements are within the legal interval. Solution
Exercise: Produce a list of all positive numbers form a given list of numbers.