Section 2 Numbers, Expressions, Simple Programs very good at working with numbers. Since teachers start their first-graders on computing wifh G In the beginning, people thought of computers as number crunchers. And indeed, computers a numbers, we start with numbers, too. Once we know how computers deal with numbers, we can develop simple programs in no time; we just translate common sense into our programming notation. Still, even developing such simple programs requires discipline, and so we introduce the outline of the most fundamental design recipe and the basic programming guideline at the end of this section 1 Numbers and arithmetic Numbers come in many different flavors: positive and negative integers, fractions(also known as ationals), and reals are the most widely known classes of numbers 5-52/317/3 135623731 The first is an integer, the second one a negative integer, the next two are fractions, and the last one is an inexact representation of a reat number Like a pocket calculator, the simplest of computers, Scheme permits programmers to add subtract, multiply, and divide numbers (+55)( The first three ask Scheme to perform additions, the last three demand a subtraction,a multiplication, and a division. All arithmetic expressions are parenthesized and mention the operation first; the numbers follow the operation and are separated by spaces As in arithmetic or algebra, we can nest expressions (*(+22)(/(*(+35)(/3010))2)) Scheme evaluates these expressions exactly as we do It first reduces the innermost parenthesized expressions to numbers, then the next layer, and so on (+22)(/ (+35)(/3010))2)) (*4(/(*83)2)) (*4(/242)) (*412) Because every Scheme expression has the shape TEAM FLY PRESENTS
-21- Section 2 Numbers, Expressions, Simple Programs In the beginning, people thought of computers as number crunchers. And indeed, computers are very good at working with numbers. Since teachers start their first-graders on computing with numbers, we start with numbers, too. Once we know how computers deal with numbers, we can develop simple programs in no time; we just translate common sense into our programming notation. Still, even developing such simple programs requires discipline, and so we introduce the outline of the most fundamental design recipe and the basic programming guideline at the end of this section. 2.1 Numbers and Arithmetic Numbers come in many different flavors: positive and negative integers, fractions (also known as rationals), and reals are the most widely known classes of numbers: 5 -5 2/3 17/3 #i1.4142135623731 The first is an integer, the second one a negative integer, the next two are fractions, and the last one is an inexact representation of a real number. Like a pocket calculator, the simplest of computers, Scheme permits programmers to add, subtract, multiply, and divide numbers: (+ 5 5) (+ -5 5) (+ 5 -5) (- 5 5) (* 3 4) (/ 8 12) The first three ask Scheme to perform additions; the last three demand a subtraction, a multiplication, and a division. All arithmetic expressions are parenthesized and mention the operation first; the numbers follow the operation and are separated by spaces. As in arithmetic or algebra, we can nest expressions: (* (+ 2 2) (/ (* (+ 3 5) (/ 30 10)) 2)) Scheme evaluates these expressions exactly as we do. It first reduces the innermost parenthesized expressions to numbers, then the next layer, and so on: (* (+ 2 2) (/ (* (+ 3 5) (/ 30 10)) 2)) = (* 4 (/ (* 8 3) 2)) = (* 4 (/ 24 2)) = (* 4 12) = 48 Because every Scheme expression has the shape TEAMFLY TEAM FLY PRESENTS
(operation A there is never any question about which part has to be evaluated first. Whenever A..B are numbers, the expression can be evaluated; otherwise, A... B are evaluated first. Contrast this 3+4·器, which is an expression that we encounter in grade school. Only a substantial amount of practice guarantees that we remember to evaluate the multiplication first and the addition afterwards Finally, Scheme not only provides simple arithmetical operations but a whole range of advanced mathematical operations on numbers. Here are five examples 1.(sqrt A) computes(A)2 (expt A B) computes A 3. (remainder A B) computes the remainder of the integer division A/B; 4.(log A)computes the natural logarithm of A; and 5. (sin A)computes the sine of A radians When in doubt whether a primitive operation exists or how it works, use DrScheme to test whether an operation is available with a simple example A Note on Numbers: Scheme computes with ExAcrintegers and rationals as long as we use primitive operations that produce exact results Thus, it displays the result of (/44 14)as 22/7 Unfortunately, Scheme and other programming languages compromise as far as real numbers are concerned. For example, since the square root of 2 is hot a rational but a real number, Scheme uses an INEXACT NUMBER (sart 2) #1.4142135623731 The #i notation warns the programmer that the result is an approximation of the true number Once an inexact number has become a part of a calculation, the process continues in an approximate manner. To wit (-#1.0#10.9) =#i0.09999999999999998 but (-# =#i0.10000000000002274 even though we know from mathematics that both differences should be 0. 1 and equal. Once numbers are inexact, caution is necessary This imprecision is due to the common simplification of writing down numbers like the square root of 2 or ras rational numbers. Recall that the decimal representations of these numbers ar infinitely long(without repetition). A computer, however, has a finite size, and therefore can only represent a portion of such a number. If we choose to represent these numbers as rationals 22 TEAM FLY PRESENTS
-22- (operation A ... B) there is never any question about which part has to be evaluated first. Whenever A ... B are numbers, the expression can be evaluated; otherwise, A ... B are evaluated first. Contrast this with which is an expression that we encounter in grade school. Only a substantial amount of practice guarantees that we remember to evaluate the multiplication first and the addition afterwards.4 Finally, Scheme not only provides simple arithmetical operations but a whole range of advanced mathematical operations on numbers. Here are five examples: 1. (sqrt A) computes (A) 1/2; 2. (expt A B) computes AB ; 3. (remainder A B) computes the remainder of the integer division A/B; 4. (log A) computes the natural logarithm of A; and 5. (sin A) computes the sine of A radians. When in doubt whether a primitive operation exists or how it works, use DrScheme to test whether an operation is available with a simple example. A Note on Numbers: Scheme computes with EXACT integers and rationals as long as we use primitive operations that produce exact results. Thus, it displays the result of (/ 44 14) as 22/7. Unfortunately, Scheme and other programming languages compromise as far as real numbers are concerned. For example, since the square root of 2 is not a rational but a real number, Scheme uses an INEXACT NUMBER: (sqrt 2) = #i1.4142135623731 The #i notation warns the programmer that the result is an approximation of the true number. Once an inexact number has become a part of a calculation, the process continues in an approximate manner. To wit: (- #i1.0 #i0.9) = #i0.09999999999999998 but (- #i1000.0 #i999.9) = #i0.10000000000002274 even though we know from mathematics that both differences should be 0.1 and equal. Once numbers are inexact, caution is necessary. This imprecision is due to the common simplification of writing down numbers like the square root of 2 or as rational numbers. Recall that the decimal representations of these numbers are infinitely long (without repetition). A computer, however, has a finite size, and therefore can only represent a portion of such a number. If we choose to represent these numbers as rationals TEAMFLY TEAM FLY PRESENTS
with a fixed number of digits, the representation is necessarily inexact. Intermezzo 6 will explain how inexact numbers work To focus our studies on the important concepts of computing and not on these details, the teaching languages of DrScheme deal as much as possible with numbers as precise numbers When we write 1.25, DrScheme interprets this number as a precise fraction, not as an inexact number. When DrScheme's Interactions window displays a number such as 1. 25 or 22/7, it is the result of a computation with precise rationals and fractions. Only numbers prefixed by #i are nexact representations Exercise 2.1.1 Find out whether DrScheme has operations for squaring a number; for computing the sine of an angle: and for determining the maximum of two numbers Exercise 2.1.2. Evaluate (sgrt 4),(sgrt 2), and (sgrt -1) in DrScheme. Then, find out whether DrScheme knows an operation for determining the tangent of an angle 2.2 Variables and Programs In algebra we learn to formulate dependencies between quantities using VARIABLE EXPRESSIONS.A variable is a placeholder that stands for an unknown quantity. For example, a disk of radius r has the approximate area? a42 In this expression, r stands for any positive number. Hf we now come across a disk with radius 5, we can determine its area by substituting, for ein the above formula and reducing the resulting expression to a number 314日=3145=75 More generally, expressions that contain variables are rules that describe how to compute a number when we are given values for the variables A program is such a rule. It is a rule that tells us and the computer how to produce data from some other data. Large programs consist of many small programs and combine them in some It is therefore important that programmers name each rule as they write it down. a good name for our sample expression is area-of-disk. Using this name, we would express the rule for computing the area of a disk as follows (define (area-of-disk r The two lines say that area-of-disk is a rule, that it consumes a single INPUT, called r, and that the result, or OUTPUT, is going to be(* 3.14 (*rr)) once we know what number r represents Programs combine basic operations. In our example, area-of-disk uses only one basi operation, multiplication, but defined programs may use as many operations as necessary. Once we have defined a program, we may use it as if it were a primitive operation. For each variable 23- TEAM FLY PRESENTS
-23- with a fixed number of digits, the representation is necessarily inexact. Intermezzo 6 will explain how inexact numbers work. To focus our studies on the important concepts of computing and not on these details, the teaching languages of DrScheme deal as much as possible with numbers as precise numbers. When we write 1.25, DrScheme interprets this number as a precise fraction, not as an inexact number. When DrScheme's Interactions window displays a number such as 1.25 or 22/7, it is the result of a computation with precise rationals and fractions. Only numbers prefixed by #i are inexact representations. Exercise 2.1.1. Find out whether DrScheme has operations for squaring a number; for computing the sine of an angle; and for determining the maximum of two numbers. Exercise 2.1.2. Evaluate (sqrt 4), (sqrt 2), and (sqrt -1) in DrScheme. Then, find out whether DrScheme knows an operation for determining the tangent of an angle. 2.2 Variables and Programs In algebra we learn to formulate dependencies between quantities using VARIABLE EXPRESSIONS. A variable is a placeholder that stands for an unknown quantity. For example, a disk of radius r has the approximate area5 In this expression, r stands for any positive number. If we now come across a disk with radius 5, we can determine its area by substituting 5 for r in the above formula and reducing the resulting expression to a number: More generally, expressions that contain variables are rules that describe how to compute a number when we are given values for the variables. A program is such a rule. It is a rule that tells us and the computer how to produce data from some other data. Large programs consist of many small programs and combine them in some manner. It is therefore important that programmers name each rule as they write it down. A good name for our sample expression is area-of-disk. Using this name, we would express the rule for computing the area of a disk as follows: (define (area-of-disk r) (* 3.14 (* r r))) The two lines say that area-of-disk is a rule, that it consumes a single INPUT, called r, and that the result, or OUTPUT, is going to be (* 3.14 (* r r)) once we know what number r represents. Programs combine basic operations. In our example, area-of-disk uses only one basic operation, multiplication, but defined programs may use as many operations as necessary. Once we have defined a program, we may use it as if it were a primitive operation. For each variable TEAMFLY TEAM FLY PRESENTS
listed to the right of the program name, we must supply one input. That is, we may write expressions whose operation is area-of-disk followed by a number (area-of-disk 5 We also say that we APPLY area-of-disk to 5 The application of a defined operation is evaluated by copying the expression named area-of disk and by replacing the variable(r)with the number we supplied (5) (area-of-disk 5 =(*3.14(*55)) (*3.1425) =78.5 Many programs consume more than one input. Say we wish to define a program that computes the area of a ring that is. a disk with a hole in the center The area of the ring is that of the outer disk minus the area of the inner disk, which means that the program requires two unknown quantities. the outer and the inner radii. Let us call these unknown numbers outer and inner. Then the program that computes the area of a ring is defined as follows (define (a ea-ofzning outer inner) (-(area-of>disk outer (area-of<disk inher The three lines express that area-of-ring is a program, that the program accepts two inputs, called outer and inner, and that the result is going to be the difference between(area-of-disk outer)and (area-of-disk inner). In other words, we have used both basic Scheme operations and defined programs in the definition of area-of-ring When we wish to use area-of-ring, we must supply two inputs (area-of-ring 5 3) The expression is evaluated in the same manner as (area-of-disk 5). We copy the expression from the definition of the program and replace the variable with the numbers we supplied (area-of-ring 5 (-(area-of-disk 5) (area-of-disk 3)) =(-(*3.14(*5 (*3.14(*3 24- TEAM FLY PRESENTS
-24- listed to the right of the program name, we must supply one input. That is, we may write expressions whose operation is area-of-disk followed by a number: (area-of-disk 5) We also say that we APPLY area-of-disk to 5. The application of a defined operation is evaluated by copying the expression named area-ofdisk and by replacing the variable (r) with the number we supplied (5): (area-of-disk 5) = (* 3.14 (* 5 5)) = (* 3.14 25) = 78.5 Many programs consume more than one input. Say we wish to define a program that computes the area of a ring, that is, a disk with a hole in the center: The area of the ring is that of the outer disk minus the area of the inner disk, which means that the program requires two unknown quantities: the outer and the inner radii. Let us call these unknown numbers outer and inner. Then the program that computes the area of a ring is defined as follows: (define (area-of-ring outer inner) (- (area-of-disk outer) (area-of-disk inner))) The three lines express that area-of-ring is a program, that the program accepts two inputs, called outer and inner, and that the result is going to be the difference between (area-of-disk outer) and (area-of-disk inner). In other words, we have used both basic Scheme operations and defined programs in the definition of area-of-ring. When we wish to use area-of-ring, we must supply two inputs: (area-of-ring 5 3) The expression is evaluated in the same manner as (area-of-disk 5). We copy the expression from the definition of the program and replace the variable with the numbers we supplied: (area-of-ring 5 3) = (- (area-of-disk 5) (area-of-disk 3)) = (- (* 3.14 (* 5 5)) (* 3.14 (* 3 3))) TEAMFLY TEAM FLY PRESENTS
The rest is plain arithmetic Exercise 2.2.1. Define the program Fahrenheit->Celsius, which consumes a temperature measured in Fahrenheit and produces the Celsius equivalent. Use a chemistry or physics book to look up the conversion formula When the function is fully developed, test it using the teachpack convert ss. The teachpack provides three functions: convert-gui, convert-repl, and convert-file. The first creates a graphical user interface. Use it with (convert-gui Fahrenheit->Celsius The expression will create a new window in which users can manipulate a slider and buttons The second emulates the Interactions window Users are asked to enter a Fahrenheit temperature, which the program reads, evaluates, and prints. Use it via (convert-repl Fahrenheit->Celsius) The last operation processes entire files. To use it, create a file with those numbers that are to be converted. Separate the numbers with blank spaces or new limes. The function reads the entire file, converts the numbers, and writes the results into anew file. Here is the expression (convert-file in dat" Fahrenheit out. dat) This assumes that the name of the newly created file is in dat and that we wish the results to be written to the file out. dat. For more information,use DrScheme's Help Desk to look up the teachpack convertss Exercise 2.2.2. Define the program dollar->euro, which consumes a number of dollars and produces the euro equivalent Use the currency table in the newspaper to look up the current exchange rate Exercise 2.2.3. Define the program triangle. It consumes the length of a triangle's side and its height. The program produces the area of the triangle. Use a geometry book to look up the formula for computing the area of a triangle Exercise 2. 2. 4. Define the program convert It consumes three digits, starting with the least significant digit, followed by the next most significant one, and so on. The program produces the corresponding number. For example, the expected value of (converts 1 s 321. Use an algebra book to find out how such a conversion works Exercise 2.2.5. A typical exercise in an algebra book asks the reader to evaluate an expression like 25- TEAM FLY PRESENTS
-25- = ... The rest is plain arithmetic. Exercise 2.2.1. Define the program Fahrenheit->Celsius, 6 which consumes a temperature measured in Fahrenheit and produces the Celsius equivalent. Use a chemistry or physics book to look up the conversion formula. When the function is fully developed, test it using the teachpack convert.ss. The teachpack provides three functions: convert-gui, convert-repl, and convert-file. The first creates a graphical user interface. Use it with (convert-gui Fahrenheit->Celsius) The expression will create a new window in which users can manipulate a slider and buttons. The second emulates the Interactions window. Users are asked to enter a Fahrenheit temperature, which the program reads, evaluates, and prints. Use it via (convert-repl Fahrenheit->Celsius) The last operation processes entire files. To use it, create a file with those numbers that are to be converted. Separate the numbers with blank spaces or new lines. The function reads the entire file, converts the numbers, and writes the results into a new file. Here is the expression: (convert-file "in.dat" Fahrenheit->Celsius "out.dat") This assumes that the name of the newly created file is in.dat and that we wish the results to be written to the file out.dat. For more information, use DrScheme's Help Desk to look up the teachpack convert.ss. Exercise 2.2.2. Define the program dollar->euro, which consumes a number of dollars and produces the euro equivalent. Use the currency table in the newspaper to look up the current exchange rate. Exercise 2.2.3. Define the program triangle. It consumes the length of a triangle's side and its height. The program produces the area of the triangle. Use a geometry book to look up the formula for computing the area of a triangle. Exercise 2.2.4. Define the program convert3. It consumes three digits, starting with the least significant digit, followed by the next most significant one, and so on. The program produces the corresponding number. For example, the expected value of (convert3 1 2 3) is 321. Use an algebra book to find out how such a conversion works. Exercise 2.2.5. A typical exercise in an algebra book asks the reader to evaluate an expression like TEAMFLY TEAM FLY PRESENTS