[an error occurred while processing this directive] [an error occurred while processing this directive]
![]() |
|
These questions are to be asked continually throughout the whole design process.
A Scheme program consists of one or more functions. Each function should have the following:
Refer to the movie theater example in section 3.1 of HTDP.
As discussed in the above analysis, the problem is to write a function that takes in a ticket price as input and produces the corresponding profit. So let's apply the design recipe to code this function. (We will do this interactively in class.)
This is what the function looks like after going through the design recipe.
;; Contract: profit : number -> number ;; ;; Header: (define (profit ticket-price) ...) ;; ;; Purpose: (profit tp) computes and returns the profit as the difference between revenue and costs ;; at some given ticket-price tp. ;; ;; Examples (TO DO): ;; (profit 5) = ? ;; (profit 4.9) = ? ;; (profit 4.8) = ? ;; (profit 4) = ? ;; ;; Definition: this is the real code. (define (profit ticket-price) (- (revenue ticket-price) (cost ticket-price))) ;; ;; Tests (TO DO) (= (profit 5) ?) (= (profit 4.9) ?) (= (profit 4.8) ?) (= (profit 4) ?)
Note that in the above, the Examples section and the Tests section are incomplete. We need to work out by hand the listed examples in order to completely fill out the Examples and Tests section. In working out the examples by hand, we should gain some deeper understanding of the problem and use that knowledge to solve the problem. Let's do this together in class.
Click here to download the EXCEL spreadsheet that solves the problem.
If we try to run the above program, we will get a few error message. One main reason is that the computer does not know what revenue and cost are. We need to apply the design recipe to write the code for revenue and cost as well. We will do this interactively in class.
What we have done in the above design process is to start "from the top" with the function representing the solution to the problem, then work down to the final details to obtain the final solution. This is called "top down" design. This design process is very popular and for all practical purposes, will suffice for our class.
Problem 1: The following is the labbie pay rate at the CS Department.
Write a function that produces the pay rate given the number of years of experience.
The main data in this problem are numbers representing the years of experience. It is partitioned into two parts:
To process this data, we need to have a way to check for the range in which the input data belongs. In Scheme, we use what is called a "cond."
© Dung X. Nguyen
Last revised 08/26/2007 12:00 AM