for n=2, n=5, and n=9. Using Scheme, we can formulate such an expression as a program and use the program as many times as necessary. Here is the program that corresponds to the above expression (define (f n) (+(/n3)2)) First determine the result of the expression at n=2, n=5, and n=9 by hand, then with DrScheme's stepper Also formulate the following three expressions as pr ograms 1.n2+10 2.(1/2)·n2+20 3.2-(1/n) Determine their results for n=2 and n=9 by hand and with DrScheme 2.3 Word Problems Programmers are rarely handed mathematical expressions to turn into-programs Instead they typically receive informal problem descriptions that often contain irrelevant and sometime: ambiguous information. The programmers' first task is to extract the relevant information and then to formulate appropriate expressions Here is a typical example 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 The last sentence is the first to mention the actual task to write a program that determines one quantity based on some other quantity. More specifically, the program consumes one quantity, the number of hours of work, and produces another one, the wage in dollars. The first sentence implies how to compute the result, but doesn't state it explicitly. In this particular example, though, this poses no problem. If an employee works h hours, the wage Now that we have a rule we can formulate a scheme program (define (wage h) (*12h)) The program is called wage; its parameter h stands for the hours an employee works; and its result is(* 12 h), the corresponding wage Exercise 2.3.1. Utopia's tax accountants always use programs that compute income taxes even though the tax rate is a solid, never-changing 15%. Define the program tax, which determines the tax on the gross pay 26- TEAM FLY PRESENTS
-26- for n = 2, n = 5, and n = 9. Using Scheme, we can formulate such an expression as a program and use the program as many times as necessary. Here is the program that corresponds to the above expression: (define (f n) (+ (/ n 3) 2)) First determine the result of the expression at n = 2, n = 5, and n = 9 by hand, then with DrScheme's stepper. Also formulate the following three expressions as programs: 1. n 2 + 10 2. (1/2) · n 2 + 20 3. 2 - (1/n) Determine their results for n = 2 and n = 9 by hand and with DrScheme. 2.3 Word Problems Programmers are rarely handed mathematical expressions to turn into programs. Instead they typically receive informal problem descriptions that often contain irrelevant and sometimes ambiguous information. The programmers' first task is to extract the relevant information and then to formulate appropriate expressions. Here is a typical example: 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. The last sentence is the first to mention the actual task: to write a program that determines one quantity based on some other quantity. More specifically, the program consumes one quantity, the number of hours of work, and produces another one, the wage in dollars. The first sentence implies how to compute the result, but doesn't state it explicitly. In this particular example, though, this poses no problem. If an employee works h hours, the wage is Now that we have a rule, we can formulate a Scheme program: (define (wage h) (* 12 h)) The program is called wage; its parameter h stands for the hours an employee works; and its result is (* 12 h), the corresponding wage. Exercise 2.3.1. Utopia's tax accountants always use programs that compute income taxes even though the tax rate is a solid, never-changing 15%. Define the program tax, which determines the tax on the gross pay. TEAMFLY TEAM FLY PRESENTS
Also define netpay. The program determines the net pay of an employee from the number of hours worked. Assume an hourly rate of $12 Exercise 2.3. 2. The local supermarket needs a program that can compute the value of a bag of coins. Define the program sum-coins It consumes four numbers: the number of pennies, nickels, dimes, and quarters in the bag; it produces the amount of money in the bag Exercise 2.3.3. 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 2. 4 Errors When we write Scheme programs, we must follow a few carefully designed rules, which are a compromise between a computer's capabilities and human behavior. Fortunately, forming Scheme definitions and expressions is intuitive. Expressions are either ATOMIC, that is, numbers and variables, or they are CoMPOUND expressions, in which case they start with"(", followed by an operation, some more expressions, and terminated by")". Each expression in a compound expression should be preceded by at least one space; line breaks are permissible, and sometimes increase readability Definitions have the following schematic shape (define (f x .. y) an-expression) That is, a definition is a sequence of several words and expressions: " (", the word"define!","(" a non-empty sequence of names separated by spaces,")",an expression,and a closing")".The embedded sequence of names introduces the name of the program and the names of Syntax Errors: Not all parenthesized expressions are Scheme expressions. For example,(10) is a parenthesized expression, but Scheme does not accept it as a legal Scheme expression because numbers are not supposed to be included in parentheses. Similarly, a sentence like(10 20)is also ill formed Scheme's rules demand that the operator is mentioned first. Finally, the following two definitions are not well formed (define (P x) (+(x)10)) (define (Q x The first one contains an extra pair of parentheses around the variable x, which is not a compound expression; the second contains two atomic expressions, x and 10, instead of one When we click DrScheme's Execute button, the programming environment first determines whether the definitions are formed according to Scheme's rules If some part of the program in the Definitions window is ill formed, DrScheme signals a SYNTAX ERROR with an appropriate 27- TEAM FLY PRESENTS
-27- Also define netpay. The program determines the net pay of an employee from the number of hours worked. Assume an hourly rate of $12. Exercise 2.3.2. The local supermarket needs a program that can compute the value of a bag of coins. Define the program sum-coins. It consumes four numbers: the number of pennies, nickels, dimes, and quarters in the bag; it produces the amount of money in the bag. Exercise 2.3.3. 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. 2.4 Errors When we write Scheme programs, we must follow a few carefully designed rules, which are a compromise between a computer's capabilities and human behavior.7 Fortunately, forming Scheme definitions and expressions is intuitive. Expressions are either ATOMIC, that is, numbers and variables; or they are COMPOUND expressions, in which case they start with ``('', followed by an operation, some more expressions, and terminated by ``)''. Each expression in a compound expression should be preceded by at least one space; line breaks are permissible, and sometimes increase readability. Definitions have the following schematic shape: (define (f x ... y) an-expression) That is, a definition is a sequence of several words and expressions: ``('', the word ``define'', ``('', a non-empty sequence of names separated by spaces, ``)'', an expression, and a closing ``)''. The embedded sequence of names, f x ... y, introduces the name of the program and the names of its parameters. Syntax Errors:8 Not all parenthesized expressions are Scheme expressions. For example, (10) is a parenthesized expression, but Scheme does not accept it as a legal Scheme expression because numbers are not supposed to be included in parentheses. Similarly, a sentence like (10 + 20) is also ill formed; Scheme's rules demand that the operator is mentioned first. Finally, the following two definitions are not well formed: (define (P x) (+ (x) 10)) (define (Q x) x 10) The first one contains an extra pair of parentheses around the variable x, which is not a compound expression; the second contains two atomic expressions, x and 10, instead of one. When we click DrScheme's Execute button, the programming environment first determines whether the definitions are formed according to Scheme's rules. If some part of the program in the Definitions window is ill formed, DrScheme signals a SYNTAX ERROR with an appropriate TEAMFLY TEAM FLY PRESENTS
error message and highlights the offending part. Otherwise it permits the user to evaluate expressions in the Interactions window Exercise 2.4.1. Evaluate the following sentences in DrScheme, one at a time (+(10)20) (10+20) Read and understand the error messages Exercise 2.4.2. Enter the following sentences, one by one, into DrScheme's Definitions window and click Execute (+x10)) (define (g x) 10) (define h(x) (+x10)) Read the error messages, fix the offending definition in an appropriate manner, and repeat until all definitions are legal Run-time Errors: The evaluation of Scheme expressions proceeds according to the intuitive laws of algebra and arithmetic. When we encounter new operations, we will extend these laws, first intuitively and then, in section 8, rigorously. For how, it is more important to understand that not all legal Scheme expressions have a result One obvious example is (/ 1 0). Similarly, if we define (define (fn (+(/n3)2)) we cannot ask DrScheme to evaluate (f 5 8) When the evaluation of a legal Scheme expression demands a division by zero or similarly nonsensical arithmetic operations, or when a program is applied to the wrong number of inputs, DrScheme stops the evaluation and signals a RUN-TIME ERROR. Typically it prints an explanation in the Interactions window and highlights the faulty expression. The highlighted expression triggered the error signal Exercise 2.4.3. Evaluate the following grammatically legal Scheme expressions in DrScheme's Interactions window (+5(/10)) (sin1020) Read the error messages 28- TEAM FLY PRESENTS
-28- error message and highlights the offending part. Otherwise it permits the user to evaluate expressions in the Interactions window. Exercise 2.4.1. Evaluate the following sentences in DrScheme, one at a time: (+ (10) 20) (10 + 20) (+ +) Read and understand the error messages. Exercise 2.4.2. Enter the following sentences, one by one, into DrScheme's Definitions window and click Execute: (define (f 1) (+ x 10)) (define (g x) + x 10) (define h(x) (+ x 10)) Read the error messages, fix the offending definition in an appropriate manner, and repeat until all definitions are legal. Run-time Errors: The evaluation of Scheme expressions proceeds according to the intuitive laws of algebra and arithmetic. When we encounter new operations, we will extend these laws, first intuitively and then, in section 8, rigorously. For now, it is more important to understand that not all legal Scheme expressions have a result. One obvious example is (/ 1 0). Similarly, if we define (define (f n) (+ (/ n 3) 2)) we cannot ask DrScheme to evaluate (f 5 8). When the evaluation of a legal Scheme expression demands a division by zero or similarly nonsensical arithmetic operations, or when a program is applied to the wrong number of inputs, DrScheme stops the evaluation and signals a RUN-TIME ERROR. Typically it prints an explanation in the Interactions window and highlights the faulty expression. The highlighted expression triggered the error signal. Exercise 2.4.3. Evaluate the following grammatically legal Scheme expressions in DrScheme's Interactions window: (+ 5 (/ 1 0)) (sin 10 20) (somef 10) Read the error messages. TEAMFLY TEAM FLY PRESENTS
Exercise 2.4.4. Enter the following grammatically legal Scheme program into the Definitions window and click the Execute button (define (some x) (sin xx)) Then, in the Interactions window, evaluate the expressions ( some 10 20) ( some 10) and read the error messages. Also observe what DrScheme highlights Logical Errors: a good programming environment assists the programmer in finding syntax and runtime errors. The exercises in this section illustrate how drScheme catches syntax and run-time errors. A programmer, however, can also make LOGICAL ERRORS. A logical mistake does not trigger any error messages; instead, the program computes incorrect results. Consider the age program from the preceding section. If the programmer had accidentally defined it as (define (wage h) the program would still produce a number every time it is used. Indeed, if we evaluate(wage 12/11), we even get the correct result. A programmer can eatch Such mistakes only by designing programs carefully and systematically 2.5 Designing Programs determine what's relevan in the roblem statement and whar arm requires many steps. We need to e can ignore. We need to understand what the program consumes, what it produces, and how it relates inputs to outputs We must know, or find out, whether Scheme provides certain basic operations for the data that our program is to process. If not, we might have to develop auxiliary programs that implement these operations. Finally, once we have a program, we must check whether it actually performs the intended computation. This might reveal syntax errors, run-time problems, or even logical errors To bring some order to this apparent chaos, it is best to set up and to follow a DESIGN RECIPE, that is, a step-by-step prescription of what we should do and the order in which we should do things Based on what we have experienced thus far, the development of a program requires at least the following four activit Contract: area-of-ring number number - number Purpose: to compute the area of a ring whose radius is outer and whose hole has a radius of inner define (ar ing outer inner 29- Flyhtear TEAM FLY PRESENTS
-29- Exercise 2.4.4. Enter the following grammatically legal Scheme program into the Definitions window and click the Execute button: (define (somef x) (sin x x)) Then, in the Interactions window, evaluate the expressions: (somef 10 20) (somef 10) and read the error messages. Also observe what DrScheme highlights. Logical Errors: A good programming environment assists the programmer in finding syntax and runtime errors. The exercises in this section illustrate how DrScheme catches syntax and run-time errors. A programmer, however, can also make LOGICAL ERRORS. A logical mistake does not trigger any error messages; instead, the program computes incorrect results. Consider the wage program from the preceding section. If the programmer had accidentally defined it as (define (wage h) (+ 12 h)) the program would still produce a number every time it is used. Indeed, if we evaluate (wage 12/11), we even get the correct result. A programmer can catch such mistakes only by designing programs carefully and systematically. 2.5 Designing Programs The preceding sections show that the development of a program requires many steps. We need to determine what's relevant in the problem statement and what we can ignore. We need to understand what the program consumes, what it produces, and how it relates inputs to outputs. We must know, or find out, whether Scheme provides certain basic operations for the data that our program is to process. If not, we might have to develop auxiliary programs that implement these operations. Finally, once we have a program, we must check whether it actually performs the intended computation. This might reveal syntax errors, run-time problems, or even logical errors. To bring some order to this apparent chaos, it is best to set up and to follow a DESIGN RECIPE, that is, a step-by-step prescription of what we should do and the order9 in which we should do things. Based on what we have experienced thus far, the development of a program requires at least the following four activities: ;; Contract: area-of-ring : number number -> number ;; Purpose: to compute the area of a ring whose radius is ;; outer and whose hole has a radius of inner ;; Example: (area-of-ring 5 3) should produce 50.24 ;; Definition: [refines the header] (define (area-of-ring outer inner) TEAMFLY TEAM FLY PRESENTS
(-(area-of-disk outer) (area-of-disk inner))) Tests (area-of-ring 5 3) 50.24 Figure 3: The design recipe: A complete example Understanding the Programs Purpose: The goal of designing a program is to create a mechanism that consumes and produces data. We therefore start every program development by giving the program a meaningful name and by stating what kind of information it consumes and produces. We call this a coNtract Here is how we write down a contract for area-of-ring, one of our first programs ;; area-of-ring number number - numl The semicolons indicate that this line is a CoMMENT. The contract consists of two parts.The colon, specifies what kind of data the program consumes and what to the right of the first, to the left of the colon, states the programs name. The second it produces; the inputs are separated from the output by an arrow each input a distinct name. These names are (algebraic)variables and are referred to o Once we have a contract, we can add the HEADER It restates the program s name and gives the program's PARAMETERS Let's take a look at the contract and - header for area-of-ring (define (area-of Ying outer inner) It says that we will refer to the first input as outer and the second one as inner Finally, using the contract and the parameters, we should formulate a short PURPOSE STATEMENT for the program, that is, a brief comment of what the program is to compute For most of our programs, one or two lines will suffice; as we develop larger and larger programs, we may need to add more information to explain a programs purpose Here is the complete starting-point for our running example number number - number i to compute the area of a ring whose radius is :: outer and whose hole has a radius of inner define (area-of-ring outer inner).. Hints: If the problem statement provides a mathematical formula, the number of distinct ariables in the formula suggests how many inputs the program consumes TEAM FLY PRESENTS
-30- (- (area-of-disk outer) (area-of-disk inner))) ;; Tests: (area-of-ring 5 3) ;; expected value 50.24 Figure 3: The design recipe: A complete example • Understanding the Program's Purpose: The goal of designing a program is to create a mechanism that consumes and produces data. We therefore start every program development by giving the program a meaningful name and by stating what kind of information it consumes and produces. We call this a CONTRACT. Here is how we write down a contract for area-of-ring, one of our first programs:10 ;; area-of-ring : number number -> number The semicolons indicate that this line is a COMMENT. The contract consists of two parts. The first, to the left of the colon, states the program's name. The second, to the right of the colon, specifies what kind of data the program consumes and what it produces; the inputs are separated from the output by an arrow. Once we have a contract, we can add the HEADER. It restates the program's name and gives each input a distinct name. These names are (algebraic) variables and are referred to as the program's PARAMETERS. 11 Let's take a look at the contract and header for area-of-ring: ;; area-of-ring : number number -> number (define (area-of-ring outer inner) ...) It says that we will refer to the first input as outer and the second one as inner. Finally, using the contract and the parameters, we should formulate a short PURPOSE STATEMENT for the program, that is, a brief comment of what the program is to compute. For most of our programs, one or two lines will suffice; as we develop larger and larger programs, we may need to add more information to explain a program's purpose. Here is the complete starting-point for our running example: ;; area-of-ring : number number -> number ;; to compute the area of a ring whose radius is ;; outer and whose hole has a radius of inner (define (area-of-ring outer inner) ...) Hints: If the problem statement provides a mathematical formula, the number of distinct variables in the formula suggests how many inputs the program consumes. TEAMFLY TEAM FLY PRESENTS