Friday, March 2, 2007

How to Design Programs - 2.4 Errors

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

Exercise 2.4.1.

Evaluate the following sentences in DrScheme, one at a time:

(+ (10) 20)
(10 + 20)
(+ +)

Read and understand the error messages.

Solution

> (+ (10) 20)
function call: expected a defined name or a primitive operation name after an open parenthesis, but found a number

The number 10 shouldn't be between parentheses here.

> (10 + 20)
function call: expected a defined name or a primitive operation name after an open parenthesis, but found a number

The number 10 is at the wrong position, it should read (+ 10 20)

> (+ +)
+: this primitive operator must be applied to arguments; expected an open parenthesis before the primitive operator name

There should be a value or expression at that position (followed by a second value or expression).

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.

Solution

(define (f 1)
  (+ x 10))
__________

define: expected a name for the function's 1st argument, but found a number

The faulty 1 should be replaced by a x:

(define (f x)
  (+ x 10))

(define (g x)
  + x 10)
__________

define: expected only one expression for the function body, but found at least one extra part

There is a open parenthesis missing, and a close parenthesis should be added to the end:

(define (g x)
  (+ x 10))

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.

Solution

> (+ 5 (/ 1 0))
/: division by zero
> (sin 10 20)
sin: expects 1 argument, given 2: 10 20
> (somef 10)
reference to an identifier before its definition: somef

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.

Solution

> (somef 10 20)
somef: this procedure expects 1 argument, here it is provided 2 arguments

There should only be one argument, e.g. (somef 10)

(define (somef x)
  (sin x x))

__________

> (somef 10)
sin: expects 1 argument, given 2: 10 10

The sin primitive accepts only one argument, it should read (sin x) in the definition.

How to Design Programs - 2.3 Word Problems

Here are the exercises and my solotions of Section 2.3, called "Word Problems", in How to Design Programs.

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.

Also define netpay. The program determines the net pay of an employee from the number of hours worked. Assume an hourly rate of $12.

Solution

The gross pay is

gross = 12 * h

the tax is 15% of the gross pay, or

tax = gross * 0.15

The net pay is gross pay minus tax, or

netpay = grosspay - tax

This means that both tax and net pay are depending on the number of hours worked.

(define (grosspay h)
  (* h 12))

(define (tax h)
  (* (grosspay h) 0.15))

(define (netpay h)
  (- (grosspay h) (tax h)))

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.

Solution

This is really easy:

value = pennies * 1 + nickels * 5 + dimes * 10 + quorters * 25

or in DrScheme:

(define (sum-coins p n d q)
  (+ p (+ (* n 5) (+ (* d 10) (* q 25)))))

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.

Solution

Each performances brings in a revenue of:

revenue = attendees * 5

The performance costs are:

cost = 20 + attendees * 0.5

The profit is:

profit = revenue - cost

or in DrScheme:

(define (revenue n)
  (* n 5))
(define (cost n)
  (+ 20 (* n 0.5)))
(define (total-profit n)
  (- (revenue n) (cost n)))

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.

How to Design Programs - 2.1 Numbers and Arithmetic

Here are the exercises and my solotions of Section 2.1, called "Numbers and Arithmetic", in How to Design Programs.

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.

Solution:

In the Help Desk, Manuals, Beginning Student Language, there are these entries:

sqr : (num -> num)

purpose:
to compute the square of a number

sin : (num -> num)

purpose:
to compute the sine of a number (radians)

pi : real

purpose:
the ratio of a circle's circumference to its diameter

max : (real real ... -> real)

purpose:
to determine the largest number

Now, this would mean the following:

(define (square n)
  (sqr n))

(define (sin-angle a)
  (sin (* (* 2 pi) (/ a 360))))

(define (maximum n1 n2)
  (max n1 n2))

Running this code gives the following output:

> (square 5)
25
> (sin-angle 90)
#i1.0
> (sin (/ pi 2))
#i1.0
> (sin-angle 180)
#i1.2246467991473532e-16
> (sin pi)
#i1.2246467991473532e-16
> (maximum 3 5)
5
> (maximum -3 -5)
-3

So, there is the primitive sqr to calculate the square of a number, you need to use a formula with pi to convert from angles to radian, and there is the primitive max to calculate 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.

Solution:

> (sqrt 4)
2
> (sqrt 2)
#i1.4142135623730951
> (sqrt -1)
0+1i
> (tan (* (* 2 pi) (/ 45 360)))
#i0.9999999999999999

Wednesday, February 28, 2007

Free Pascal with Lazarus IDE

I'm on a Mac, and installing Free Pascal with the Lazarus integrated development environment (IDE) wasn't easy, and really not very automated once installed. It uses GTK+, which has to be installed using Fink. Then three additional files have to be installed (two for Lazarus, and one for the Free Pascal compiler). Lazarus is a X11 application (X11 is equivalent to X on Linux).



I guess this is often the disadvantage of using open source software. It is somewhat more difficult to install, and the user experience is less well defined as with commercial and free software. Designing programs and designing user interfaces are two separate disciplines, few people have mastered both.

I haven't really looked into this, but the Learn Pascal Tutorial by Taoyue.com seems to be a descent introduction into the Pascal Language. Because Pascal is a compiler language, developing small programs is somewhat less comfortable than with an interactive mode, as some of the more modern languages have baked into them. Nevertheless, Pascal is the descendant of Algol, and the forerunner of a plethora of C type languages (e.g. C, C++, Objective-C, C#, etc.).

Should I mention that Free Pascal is compatible to Borland's Delphi? There was the original Pascal by Dr. Niklaus Wirth, Turbo Pascal by Borland Inc., Modula-2 (also by Wirth), and Delphi (by Borland). With both Free Pascal and Delphi it is possible to write GUI driven programs, and both compile lightning fast.

J

J is a powerful abstract language, which is a direct descender of the (in)famous programming language APL (A Programming Language). While APL uses mathematical notation with non-standard characters (which makes it look very cryptic, thus the earlier infamous reference), J uses standard ASCII characters. The free development software (IDE) is governed by JSoftware. It has a graphical user interface and good documentation--even for novice programmers--built in.

Common Lisp with IDE

Through a comment (found through Google) on Lambda the Ultimate I found an integrated development environment (IDE) for Lisp, called LispWorks Personal Edition. It is the free version of a commercial product, which has a few limitations (limited heap stack, five hours continuous operation, after which it shuts down, giving warning after four hours). This is perfect for learning Lisp, at least, for me who is used to GUI environments. I have CMU-Lisp on the command line, but I have no experience with Emacs, which seems to be a prerequisite for effectively programming CMU-Lisp.

A good resource for learning Lisp seems to be Successful Lisp: How to Understand and Use Common Lisp, by David B. Lamkins.