section 02 numbers, expressions, simple programs version 1.1.0 prepared by: it group last modified:...

25
Section 02 Numbers, Expressions, Simple Programs Version 1.1.0 Prepared by: IT Group Last modified: Apr 04, 2008

Upload: piers-edwards

Post on 06-Jan-2018

215 views

Category:

Documents


0 download

DESCRIPTION

1. Numbers A positive integer: 5 A negative integer: -5 Fractions: 2/3, 17/3 Real Number : 2.33, 1.56

TRANSCRIPT

Page 1: Section 02 Numbers, Expressions, Simple Programs Version 1.1.0 Prepared by: IT Group Last modified: Apr 04, 2008

Section 02

Numbers, Expressions, Simple Programs

Version 1.1.0

Prepared by: IT Group

Last modified: Apr 04, 2008

Page 2: Section 02 Numbers, Expressions, Simple Programs Version 1.1.0 Prepared by: IT Group Last modified: Apr 04, 2008

Contents1. Numbers 2. Expressions3. Variables4. Programs5. The Wage Problem6. Solution7. Exercises

Page 3: Section 02 Numbers, Expressions, Simple Programs Version 1.1.0 Prepared by: IT Group Last modified: Apr 04, 2008

1. Numbers• A positive integer: 5• A negative integer: -5• Fractions: 2/3, 17/3• Real Number: 2.33, 1.56

Page 4: Section 02 Numbers, Expressions, Simple Programs Version 1.1.0 Prepared by: IT Group Last modified: Apr 04, 2008

2. Expressions2.1. Expressions in Scheme2.2. Supported Expressions2.3. Nested Expressions2.4. Hands-On exercises

Page 5: Section 02 Numbers, Expressions, Simple Programs Version 1.1.0 Prepared by: IT Group Last modified: Apr 04, 2008

2.1. Expressions in Scheme• Scheme expression uses the prefix

notations:(operation A ... B) • Examples: (+ 12 8)

(+ 5 5) (+ -5 5)  (+ 5 -5)     (- 5 5)     (* 3 4)      (/ 8 12)

Page 6: Section 02 Numbers, Expressions, Simple Programs Version 1.1.0 Prepared by: IT Group Last modified: Apr 04, 2008

2.2. Supported Expressions

• Scheme supports some basic expressions: (sqrt A): computes (A)1/2 = (expt A B): computes AB (remainder A B): computes the remainder

of the integer division A/B (quotient A B) : Returns the integer portion

of a division …

A

Page 7: Section 02 Numbers, Expressions, Simple Programs Version 1.1.0 Prepared by: IT Group Last modified: Apr 04, 2008

2.3. Nested Expressions• As in arithmetic or algebra, expressions

can be nested: (2 + 2) * (3 – 5)

• Express above expression in Scheme? (* (+ 2 2) (- 3 5))

Page 8: Section 02 Numbers, Expressions, Simple Programs Version 1.1.0 Prepared by: IT Group Last modified: Apr 04, 2008

2.4. Hands-On Exercises

• 3 + 4 * 5• ( 2 + 2) * (3 + 5) * 30 • ( 3 + 5) * (5 + 7) * 30 / (10 + 2)• , • 52 , (2 + 1)3, 7(2 + 3) • (remainder 15 4), (remainder 27 3)• (quotient 15 4), (quotient 27 3)

4 124

Hands-on: Use DrScheme to evaluate the following expressions.

Page 9: Section 02 Numbers, Expressions, Simple Programs Version 1.1.0 Prepared by: IT Group Last modified: Apr 04, 2008

3. Variables• A variable is a placeholder that stands for

an unknown quantity. • For example, a disk has the approximate

area: 3.14 * r2

• r is a variable which stands for any positive number.

• r = 53.14 * 52 = 3.14 * 25 = 78.5

Page 10: Section 02 Numbers, Expressions, Simple Programs Version 1.1.0 Prepared by: IT Group Last modified: Apr 04, 2008

4. Programs4.1. Define and Execute a Program4.2. Hands-On Exercise

Page 11: Section 02 Numbers, Expressions, Simple Programs Version 1.1.0 Prepared by: IT Group Last modified: Apr 04, 2008

4.1 Define and Execute a Program• A program is such a rule that tells us and the

computer how to produce data from some other data.

• Program definition in Scheme:

• Execute a Program:

Page 12: Section 02 Numbers, Expressions, Simple Programs Version 1.1.0 Prepared by: IT Group Last modified: Apr 04, 2008

4.2. Hands-on ExerciseA. Define the program dollarToEuro, which

consumes a number of dollars and produces the euro equivalent. The current exchange rate is 1.5

B. Define a program getCircleArea. It consumes the radius of a circle and produces the area of the circle.

Page 13: Section 02 Numbers, Expressions, Simple Programs Version 1.1.0 Prepared by: IT Group Last modified: Apr 04, 2008

5. The Wage Problem

The New Age Inc. company 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.

Page 14: Section 02 Numbers, Expressions, Simple Programs Version 1.1.0 Prepared by: IT Group Last modified: Apr 04, 2008

6. Solution

Page 15: Section 02 Numbers, Expressions, Simple Programs Version 1.1.0 Prepared by: IT Group Last modified: Apr 04, 2008

Step 1: Problem description

Page 16: Section 02 Numbers, Expressions, Simple Programs Version 1.1.0 Prepared by: IT Group Last modified: Apr 04, 2008

Step 2: Function name

Page 17: Section 02 Numbers, Expressions, Simple Programs Version 1.1.0 Prepared by: IT Group Last modified: Apr 04, 2008

Step 3: Writing test cases

Page 18: Section 02 Numbers, Expressions, Simple Programs Version 1.1.0 Prepared by: IT Group Last modified: Apr 04, 2008

Step 4: Template

Page 19: Section 02 Numbers, Expressions, Simple Programs Version 1.1.0 Prepared by: IT Group Last modified: Apr 04, 2008

Step 5: Implementation

Page 20: Section 02 Numbers, Expressions, Simple Programs Version 1.1.0 Prepared by: IT Group Last modified: Apr 04, 2008

Step 6: Testing

Page 21: Section 02 Numbers, Expressions, Simple Programs Version 1.1.0 Prepared by: IT Group Last modified: Apr 04, 2008

7. Exercises

Exercise 2.1: Rewrite program that estimates the Wage Problem above. Let’s try by yourself to solve the problem in six steps

Page 22: Section 02 Numbers, Expressions, Simple Programs Version 1.1.0 Prepared by: IT Group Last modified: Apr 04, 2008

7. Exercises (cont.)

Exercise 2.2: 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

Page 23: Section 02 Numbers, Expressions, Simple Programs Version 1.1.0 Prepared by: IT Group Last modified: Apr 04, 2008

7. Exercises (cont.)

Exercise 2.3: Calculating the area of a ring with given inner and outer radius

Page 24: Section 02 Numbers, Expressions, Simple Programs Version 1.1.0 Prepared by: IT Group Last modified: Apr 04, 2008

7. Exercises (cont.)

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

Page 25: Section 02 Numbers, Expressions, Simple Programs Version 1.1.0 Prepared by: IT Group Last modified: Apr 04, 2008

References

How to Design Programs - Section 02Matthias Felleisen, Robert Bruce Findler, Matthew Flatt,Shriram Krishnamurthihttp://htdp.org/2003-09-26/Book/curriculum-Z-H-5.html#node_chap_2