pre-scheme_s4

Section 4
Comparing and Conditional
Expressions
Version: 1.1.0
Prepared by: IT Group
Last modified: Apr 16, 2008
Contents
1.
2.
3.
4.
5.
Comparing Expressions
Conditional Expressions
The Scholarship Problem
The Scholarship Solution
Exercises
1. Comparing Expressions
1.1. Comparing Expressions in Scheme
1.2. The Result of a Comparing Expression
1.3. Some Supported Expressions
1.4. Hands-On Exercises
1.1. Comparing Expressions in
Scheme
• In Scheme, comparing expressions are written
by starting a left parenthesis, followed by the
operator, its arguments, and a right
parenthesis:
• (= x
• (< x
y'';
• (> x
y''.
y): ``x is equal to y'';
y): ``x is strictly less than
and
y): ``x is strictly greater than
1.2.Result of a Comparing Expression
• Result of a comparing expression is true
or false.
• Examples:
1.3. Some Supported Expressions
• Scheme also supports and, or, not
operators to express compound conditions
(and (= x y) (< y z))
(or (= x y) (< y z))
(not (= x y))
• Examples:
(and (= 5 5) (< 5 6)) -> true
(and (= 5 5) (< 5 5)) -> false
1.4. Hands-On Exercises
A. What are the results of the following Scheme
conditions?
1. (and (> 4 3) (<= 10 100))
2. (or (> 4 3) (= 10 100))
3. (not (= 2 3))
B. What are the results of the expressions below?
1. (> x 3)
in case x = 4
2. (and (> 4 x) (> x 3)) in case
x=2
2. Conditional Expressions
2.1. Conditional Expressions in Scheme
2.2. Hands-On Exercises
2.1. Conditional Expressions in Scheme
• Syntax:
• Example:
– If a = b, result is “equals”
– If a > b, result is “greater than”
– If a < b, result is “less than”
OR
2.2. Hands-On Exercises
A. What is the value of
(cond
[(<= n 1000) .040]
[(<= n 5000) .045]
[(<= n 10000) .055]
[(> n 10000) .060]
)
when n is (a) 500, (b) 2800, and (c) 15000?
3. Scholarship Problem
• BlueSky school wants to give scholarship for their
student. Scholarship will be awarded for following
conditions:
Conditions
Scholarship
GPA < 2.5
0$
2.5 <= GPA < 3.0
300$
3.0 <= GPA <= 4.0
350$
• With given GPA, design a program to calculate
scholarship for a specific student.
4. The Scholarship Solution
4.1. Problem Description
4.2. Function Name
4.3. Test Cases
4.4. Function Template
4.5. Implementation
4.6. Testing
4.1. Problem Description
4.2. Function Name
4.3. Test Cases
4.4. Function Template
4.5. Implementation
4.6. Testing
5. Exercises
5.1. BMI Problem
5.2. Salary Payment
5.3. Internet Fee
5.4. Power Cost
5.5. Ideal Weight
5.6. Hotel Problem
5.1. BMI Problem
• Body mass index (BMI) is a measure
of body fat based on height and
weight that applies to both adult men
and women. The formula which
determines BMI is:
BMI categories
Underweight
less than 18.5
Normal
Overweight
from 18.5 to 25
from 25 to 30
Obese level 1
from 30 to 35
Obese level 2
Super obese
from 35 to 40
greater than 40
Write a program to determine the weight
status of a person belong to his/her height
and weight.
5.2. Salary Payment
• The Blue Horizon company needs to compute
the net salary that it has to pay for each worker.
Given wage per hour, number of working hours
and bonus (if any). The income tax is calculated
as below:
• For the gross salary of $4000 or less, the tax rate is 0%.
• For the gross salary which is over $4000 and $6000 or
less, the tax rate is 15% of part which is over $4000
• For any gross salary which is over $6000, the tax is sum
of $2000 * 10% and 20% of part which is over $6000
5.3. Internet Fee
• An Internet Service Provider charges monthly
internet cost of each customer depending on
amount of megabyte (MB) transferred.
• Data transfers between 0 and 500 MB are
charged $0.05/MB. Data transfers between 500
MB and 1500 MB are charged $0.08/MB. Above
1500 MB are charged $0.1/MB. Each customer
must pay additional charge is $20.
• Develop the function which takes an amount of
data transferred in megabytes and computes the
total charge.
5.4. Power Cost
• Design a program that calculates monthly
power cost, given amount of used Kwh.
• The power cost can be calculated as
following:
– 600 vnd/kwh for the first 150 kwh.
– 1000 vnd/kwh for everything over
150kwh.
5.5. Ideal Weight
• Write a program to determine the ideal
weight of a person base on his/her height.
The formula which determines the ideal
weight is:
2
2
height (cm )
Male : IdealWeigh t =
398
2
2
height (cm )
Female : IdealWeigh t =
426
5.6. Hotel Problem
• Design a program calculates total cost (without
services) which a customer has to pay for hotel
when he/she books a room. Given the amount of
rent day and room type.
– For the first week (amount of rent days is less
than 7), total cost is:
Amount of rent days x Price/Day
– For next weeks, total cost is :
Amount of rent weeks x Price/Week +
Amount of rent days x Price/Day
The price list is below:
Room type
1
2
3
Price($)/Day
10
7
5
Price($)/Week
60
42
30
• If the number of rent days is 5 and type of room is 2. The cost is 5 x
7= $35
• If the number of rent day is 7 and type of room is 2. The cost is 1 x
42= $42
• If the number of rent day is 20 and type of room is 2. The cost is 2 x
42 + 6 x 7 = $126
References
How to Design Programs - Section 04
Matthias Felleisen, Robert Bruce Findler,
Matthew Flatt,Shriram Krishnamurthi
http://htdp.org/2003-09-26/Book/curriculum-Z-H-5.html#node_chap 4