Exercise 3.1.2. Use the results of exercise 3. 1. 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? Once we have written down the basic material about our functions and calculated out several examples, we can replace the"... " with Scheme expressions. The left column of figure 5 contains complete definitions of all four functions. The profit function computes its result as the difference between the result of revenue and cost, just as the problem analysis and purpose statement suggest. The computation of both depends on ticket-price, which is what the applications say. To compute the revenue, we first compute the number of attendees for the given ticket-price and multiply it with ticket-price. Similarly, to compute the cost we add the fixed portion of the cost to the variable part, which is the product of the number of attendees nd 0. 04(four cents ) Finally, the computation of the number of attendees also follows the problem statement. The base attendance at a price of five dollars is 120, and for each 10 cents less than five dollars, 15 more attendees show up How to design a program (define (profit ticket-price) (-(revenue ticket-price How not to design a (cost ticket-price))) define (profit price) (define (revenue ticket-price ( (attendees ticket-price) ticket ((/15.10) price)) 5.00 price))) (define (cost ticket-price) (+180 (*.04 (*.04 (attendees tide (+120 price)))) (*(/ (define(attendees tieket-price) rice))))))) (+120 price)))) Figure 5: Two ways to express the profit program Instead of developing a function per dependency in the problem statement, we could have tried to express the relationship between the ticket price and the owner's profit in a single function The right column in figure 5 shows the most straightforward way of doing so. And indeed, it is easy to check that the two profit programs in figure 5 produce the same profit when given the same ticket price. Still, it is also obvious that while the arrangement on the left conveys the intention behind the program directly, the program on the right is nearly impossible to understand. Worse, if we are asked to modify some aspect of the program, say, the relationship between the number of attendees and the price of the ticket, we can do this for the left column in a small amount of time, but we need to spend a much longer time for the right one Based on our experience, we thus formulate the first and most important guideline of prog -36- TEAM FLY PRESENTS
-36- Exercise 3.1.2. Use the results of exercise 3.1.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? Once we have written down the basic material about our functions and calculated out several examples, we can replace the ``...'' with Scheme expressions. The left column of figure 5 contains complete definitions of all four functions. The profit function computes its result as the difference between the result of revenue and cost, just as the problem analysis and purpose statement suggest. The computation of both depends on ticket-price, which is what the applications say. To compute the revenue, we first compute the number of attendees for the given ticket-price and multiply it with ticket-price. Similarly, to compute the cost we add the fixed portion of the cost to the variable part, which is the product of the number of attendees and 0.04 (four cents). Finally, the computation of the number of attendees also follows the problem statement. The base attendance at a price of five dollars is 120, and for each 10 cents less than five dollars, 15 more attendees show up. ;; How to design a program (define (profit ticket-price) (- (revenue ticket-price) (cost ticket-price))) (define (revenue ticket-price) (* (attendees ticket-price) ticketprice)) (define (cost ticket-price) (+ 180 (* .04 (attendees ticketprice)))) (define (attendees ticket-price) (+ 120 (* (/ 15 .10) (- 5.00 ticketprice)))) ;; How not to design a program (define (profit price) (- (* (+ 120 (* (/ 15 .10) (- 5.00 price))) price) (+ 180 (* .04 (+ 120 (* (/ 15 .10) (- 5.00 price))))))) Figure 5: Two ways to express the profit program Instead of developing a function per dependency in the problem statement, we could have tried to express the relationship between the ticket price and the owner's profit in a single function. The right column in figure 5 shows the most straightforward way of doing so. And indeed, it is easy to check that the two profit programs in figure 5 produce the same profit when given the same ticket price. Still, it is also obvious that while the arrangement on the left conveys the intention behind the program directly, the program on the right is nearly impossible to understand. Worse, if we are asked to modify some aspect of the program, say, the relationship between the number of attendees and the price of the ticket, we can do this for the left column in a small amount of time, but we need to spend a much longer time for the right one. Based on our experience, we thus formulate the first and most important guideline of programming: TEAMFLY TEAM FLY PRESENTS
Guideline on Auxiliary functions Formulate auxiliary function definitions for every dependency between quantities mentioned in the problem statement or discovered with example calculations Sometimes we will find that some of the required functions are already available as programs for other problems. Indeed, we have already encountered such an example: area-of-disk. At other times, we will make a list of functions and develop each one separately. We may then find that some of the functions, such as attendees, are useful in several other definitions, leading to network-like relationship among functions Exercise 3.1.3. 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 3. 1.2 Exercise 3.1.4. After studying the cost structure of a show, the owner discovered several ways Simpa ering the cost. As a result of his improvements, he no longer has a fixed cost. He now of low ly pays $1. 50 per attendee Modify both programs to reflect this change. When the programs are modified, test them again with ticket prices of $3.00, $4.00, and $5.00 and compare the results 3.2 Variable Definitions When a number occurs many times in our program(s), we should give it a name using a VARIABLE DEFINITION, which associates a name with a value. One example is 3. 14, which we have used in place of m. Here is how we could give this numbera name define PI 3. 14 Now, every time we refer to PI, DrScheme replaces it with 3.14 Using a name for a constant makes it easier to replace it with a different value. Suppose our program contains the definition for PI, and we decide that we need a better approximation of - for the entire program. By changing the definition to (define PI 3.14159 the improvement is used everywhere where we use PI. If we didn ' t have a name like pI for we would have to find and all instances of 3. 14 in the program and replace them with 3.1415 Let us formulate this observation as our second guideline Guideline on variable Definitions Give names to frequently used constants and use the names instead of the constants in programs ny variable definitions for constants, because our progran But, as we learn to write larger programs, we will make more use of variable definitions. As we TEAM FLY PRESENTS
-37- Guideline on Auxiliary Functions Formulate auxiliary function definitions for every dependency between quantities mentioned in the problem statement or discovered with example calculations. Sometimes we will find that some of the required functions are already available as programs for other problems. Indeed, we have already encountered such an example: area-of-disk. At other times, we will make a list of functions and develop each one separately. We may then find that some of the functions, such as attendees, are useful in several other definitions, leading to a network-like relationship among functions. Exercise 3.1.3. 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 3.1.2. Exercise 3.1.4. After studying the cost structure of a show, the owner discovered several ways of lowering the cost. As a result of his improvements, he no longer has a fixed cost. He now simply pays $1.50 per attendee. Modify both programs to reflect this change. When the programs are modified, test them again with ticket prices of $3.00, $4.00, and $5.00 and compare the results. 3.2 Variable Definitions When a number occurs many times in our program(s), we should give it a name using a VARIABLE DEFINITION, which associates a name with a value. One example is 3.14, which we have used in place of . Here is how we could give this number a name: (define PI 3.14) Now, every time we refer to PI, DrScheme replaces it with 3.14. Using a name for a constant makes it easier to replace it with a different value. Suppose our program contains the definition for PI, and we decide that we need a better approximation of for the entire program. By changing the definition to (define PI 3.14159) the improvement is used everywhere where we use PI. If we didn't have a name like PI for , we would have to find and all instances of 3.14 in the program and replace them with 3.14159. Let us formulate this observation as our second guideline: Guideline on Variable Definitions Give names to frequently used constants and use the names instead of the constants in programs. Initially, we won't use many variable definitions for constants, because our programs are small. But, as we learn to write larger programs, we will make more use of variable definitions. As we TEAMFLY TEAM FLY PRESENTS
will see, the ability to have a single point of control for changes is important for variable and function definitions Exercise 3. 2.1. Provide variable definitions for all constants that appear in the profit program of figure 5 and replace the constants with their names 3.3 Finger Exercises on Composing Functions Exercise 3.3.1. The United States uses the English system of (length)measurements. The rest of the world uses the metric system. So, people who travel abroad and companies that trade with foreign partners often need to convert English measurements to metric ones and vice versa Here is a table that shows the six major units of length measurements of the English system: 2 English metrI inch cm 12 in I yard =3 ft rod =5(1/2)yd 1 furlong= 40 rd I mile :8 f Develop the functions inches->pm, fept inches, yards->feet, rods->yards, furlongs- >rods and miles->furlon Then develop the funetions feet em ards->cm, rods->inches, and miles->feet Hint: Reuse functions as much as possible. Use variable definitions to specify constants Exel disk 3.3.2. Develop the program volume-cylinder. It consumes the radius of a cylinder's and its height; it computes the volume of the cylinder Exercise 3.3.3. Develop linder. The program consumes the radius of the cylinder's base disk and its height. Its result is the surface area of the cylinder Exercise 3. 3. 4. Develop 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? Exercise 3.3.5. Develop the program height, which computes the height that a rocket reaches in a given amount of time. If the rocket accelerates at a constant rate g, it reaches a speed of g t in t time units and a height of 1/2*v* t where v is the speed at t -38- TEAM FLY PRESENTS
-38- will see, the ability to have a single point of control for changes is important for variable and function definitions. Exercise 3.2.1. Provide variable definitions for all constants that appear in the profit program of figure 5 and replace the constants with their names. 3.3 Finger Exercises on Composing Functions Exercise 3.3.1. The United States uses the English system of (length) measurements. The rest of the world uses the metric system. So, people who travel abroad and companies that trade with foreign partners often need to convert English measurements to metric ones and vice versa. Here is a table that shows the six major units of length measurements of the English system:12 English metri c 1 inch = 2.54 cm 1 foot = 12 in. 1 yard = 3 ft. 1 rod = 5(1/2) yd. 1 furlong = 40 rd. 1 mile = 8 fl. Develop the functions inches->cm, feet->inches, yards->feet, rods->yards, furlongs- >rods, and miles->furlongs. Then develop the functions feet->cm, yards->cm, rods->inches, and miles->feet. Hint: Reuse functions as much as possible. Use variable definitions to specify constants. Exercise 3.3.2. 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. Exercise 3.3.3. Develop 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. Exercise 3.3.4. Develop 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? Exercise 3.3.5. Develop the program height, which computes the height that a rocket reaches in a given amount of time. If the rocket accelerates at a constant rate g, it reaches a speed of g · t in t time units and a height of 1/2 * v * t where v is the speed at t. TEAMFLY TEAM FLY PRESENTS
consumes a temperature measured in Fahrenheit and produces the Celsius equivalent: brogram Exercise 3.3.6. Recall the program Fahrenheit->celsius from exercise 2. 2. 1. The Develop the program celsius->Fahrenheit, which consumes a temperature measured in Celsius and produces the Fahrenheit equivalent Now consider the function number - number to convert a Fahrenheit temperature to Celsiu ( Celsius->Fahrenheit ( Fahrenheit->Celsius f)) Evaluate(I 32) by hand and using DrScheme's stepper. What does this suggest about the composition of the two functions? See The World Book Encyclopedia 1993, Weights and Measurements -39 TEAM FLY PRESENTS
-39- Exercise 3.3.6. Recall the program Fahrenheit->Celsius from exercise 2.2.1. The program consumes a temperature measured in Fahrenheit and produces the Celsius equivalent. Develop the program Celsius->Fahrenheit, which consumes a temperature measured in Celsius and produces the Fahrenheit equivalent. Now consider the function ;; I : number -> number ;; to convert a Fahrenheit temperature to Celsius and back (define (I f) (Celsius->Fahrenheit (Fahrenheit->Celsius f))) Evaluate (I 32) by hand and using DrScheme's stepper. What does this suggest about the composition of the two functions? 12 See The World Book Encyclopedia 1993, Weights and Measurements. TEAMFLY TEAM FLY PRESENTS
Section 4 Conditional Expressions and Functions For many problems, computer programs must deal with different situations in different ways. A located in some specific area of the screen. For an engine control program, a condition may it is game program may have to determine whether an object's speed is in some range or whether describe whether or when a valve is to be opened. To deal with conditions, we need to have a way of saying a condition is true or false; we need a new class of values, which, by convention, are called BOOLEAN(or truth) values. This section introduces booleans, expressions that evaluate to Booleans, and expressions that compute values depending on the boolean result of some evaluation 4.1 Booleans and relations Consider the following problem statement Company XYZ Co pays all its employees $12 per hour Atypicalemployee works between 20 and 65 hours per week. Develop a program that determines the wage of an employee from the number of hours of work, if the number is within he properrange The italic words highlight the new part (compared tol section 2.3). They imply that the program must deal with its input in one way if it is in the legitimate range, and in a different way if it is not In short, just as people need to reasbpr about conditions, programs must compute in a conditional manner Conditions are nothing new. In mathematics we talk of true and false claims, which are conditions. For example, a number may be equal to, less than, or greater than some other number If x and y are numbers, we state these three claims about x and y with 1. x=y: x is equal to y 2. x<y:x is strictly less than y 3. x>y: x is strictly greater than y For any specific pair of (real)numbers, exactly one of these claims holds. If x=4 and y=5, the second claim is a true statement, and the others are false Ifx=5 and y= 4, however, the third claim is true, and the others are false. In general, a claim is true for some values of the variables and false for others In addition to determining whether an atomic claim holds in a given situation, it is sometimes important to determine whether combinations of claims hold. Consider the three claims above which we can combine in several ways 1. x=y and x<y and x>y 2. x=y or x<y or x>y TEAM FLY PRESENTS
-40- Section 4 Conditional Expressions and Functions For many problems, computer programs must deal with different situations in different ways. A game program may have to determine whether an object's speed is in some range or whether it is located in some specific area of the screen. For an engine control program, a condition may describe whether or when a valve is to be opened. To deal with conditions, we need to have a way of saying a condition is true or false; we need a new class of values, which, by convention, are called BOOLEAN (or truth) values. This section introduces booleans, expressions that evaluate to Booleans, and expressions that compute values depending on the boolean result of some evaluation. 4.1 Booleans and Relations Consider the following problem statement: Company XYZ & Co. pays all its employees $12 per hour. A typical employee works between 20 and 65 hours per week. Develop a program that determines the wage of an employee from the number of hours of work, if the number is within the proper range. The italic words highlight the new part (compared to section 2.3). They imply that the program must deal with its input in one way if it is in the legitimate range, and in a different way if it is not. In short, just as people need to reason about conditions, programs must compute in a conditional manner. Conditions are nothing new. In mathematics we talk of true and false claims, which are conditions. For example, a number may be equal to, less than, or greater than some other number. If x and y are numbers, we state these three claims about x and y with 1. x = y: ``x is equal to y''; 2. x < y: ``x is strictly less than y''; 3. x > y: ``x is strictly greater than y''. For any specific pair of (real) numbers, exactly one of these claims holds. If x = 4 and y = 5, the second claim is a true statement, and the others are false. If x = 5 and y = 4, however, the third claim is true, and the others are false. In general, a claim is true for some values of the variables and false for others. In addition to determining whether an atomic claim holds in a given situation, it is sometimes important to determine whether combinations of claims hold. Consider the three claims above, which we can combine in several ways: 1. x = y and x < y and x > y 2. x = y or x < y or x > y TEAMFLY TEAM FLY PRESENTS