Thursday, March 1, 2007

How to Design Programs - 2.2 Variables and Programs

Here are the exercises and my solotions of Section 2.2, called "Variables and Programs", in How to Design Programs.

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.

solution

I found a formula here:

(F-32)*5/9 = C

Expressing this in DrScheme:

(define (Fahrenheit->Celsius F)
  (* (- F 32) (/ 5 9)))

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.

Solution

Type this into Google

1 dollar in euros

The result was:

1 U.S. dollar = 0.757002271 Euros

So we need to multiply the amount of dollars with 0.757002271:

(define (dollar->euro D)
  (* D 0.757002271))

Exercise 2.2.3.

Define the program triangle. It consumes the length of a triangle's side and the perpendicular 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.

Solution

According to this math page, the area of a triangle is calculated as follows:

A = (w * h) / 2

This means expressed in DrSchema, as a function:

(define (triangle w h)
  (/ (* w h) 2))

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.

Solution

According to this page, a number like 123 can be expressed as follows:

123 = 1 * 100 + 2 * 10 + 3

So, if the digits are given in the reverse order, 3, 2, 1, then the first value should be multiplied by 100, the second with 10, and the products should be added together with the third digit. This gives the following program:

(define (convert3 n1 n2 n3)
  (+ n1 (+ (* 10 n2) (* 100 n3))))

Exercise 2.2.5.

A typical exercise in an algebra book asks the reader to evaluate an expression like

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. 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.

Solution

For n = 2, n = 5, n = 9,

is 2(2/3), 3(2/3), and 5. With Stepper the results are 8/3, 11/3, and 5.

For n = 2 and n = 9 in n2 + 10, the results are 14 and 91.

(define (g n)
  (+ (sqr n) 10))

gives:

> (g 2)
14
> (g 9)
91

For n = 2 and n = 9 in (1/2) · n2 + 20, the results are 22 and 60(1/2).

(define (h n)
  (+ (* 1/2 (sqr n)) 20))

gives:

> (h 2)
22
> (h 9)
60.5

For n = 2 and n = 9 in 2 - (1/n), the results are 1(1/2) and 1(8/9).

(define (j n)
  (- 2 (/ 1 n)))

gives:

> (j 2)
1.5
> (j 9)
1.8

which are all correct.

No comments: