Download Chapter 2 - SaigonTech

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Section 02
Numbers, Expressions,
Simple Programs
Version 1.1.0
Prepared by: IT Group
Last modified: Apr 04, 2008
Contents
1.
2.
3.
4.
5.
Numbers
Expressions
Variables
Programs
The Wage Problem
6. Solution
7. Exercises
1. Numbers
•
•
•
•
A positive integer: 5
A negative integer: -5
Fractions: 2/3, 17/3
Real Number: 2.33, 1.56
2. Expressions
2.1. Expressions in Scheme
2.2. Supported Expressions
2.3. Nested Expressions
2.4. Hands-On exercises
2.1. Expressions in Scheme
• Scheme expression uses the prefix
notations:
(operation A ... B)
• Examples:
(+ 12 8)
(+
((*
(/
5
5
3
8
5) (+ -5 5)
5)
4)
12)
(+ 5 -5)
2.2. Supported Expressions
•
Scheme supports some basic
expressions:





A
(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
…
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
Hands-on: Use DrScheme to evaluate the following
expressions.
•
•
•
•
•
•
•
3 + 4 * 5
( 2 + 2) * (3 + 5) * 30
( 3 + 5) * (5 + 7) * 30 / (10 + 2)
4 , 4  12
52 , (2 + 1)3, 7(2 + 3)
(remainder 15 4), (remainder 27 3)
(quotient 15 4), (quotient 27 3)
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 = 5
3.14 * 52 = 3.14 * 25 = 78.5
4. Programs
4.1. Define and Execute a Program
4.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 Exercise
A. 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 02
Matthias Felleisen, Robert Bruce Findler,
Matthew Flatt,Shriram Krishnamurthi
http://htdp.org/2003-09-26/Book/curriculum-Z-H-5.html#node_chap_2
Related documents