section 02 numbers, expressions, simple programs version 1.1.0 prepared by: it group last modified:...
DESCRIPTION
1. Numbers A positive integer: 5 A negative integer: -5 Fractions: 2/3, 17/3 Real Number : 2.33, 1.56TRANSCRIPT
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
1. Numbers• A positive integer: 5• A negative integer: -5• Fractions: 2/3, 17/3• Real Number: 2.33, 1.56
2. Expressions2.1. Expressions in Scheme2.2. Supported Expressions2.3. Nested Expressions2.4. Hands-On exercises
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)
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
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))
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.
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
4. Programs4.1. Define and Execute a Program4.2. Hands-On Exercise
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:
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.
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.
6. Solution
Step 1: Problem description
Step 2: Function name
Step 3: Writing test cases
Step 4: Template
Step 5: Implementation
Step 6: Testing
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
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
7. Exercises (cont.)
Exercise 2.3: Calculating the area of a ring with given inner and outer radius
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.
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