1 / 29

Scheme Comparing and Conditional Expressions

Learn how to use comparing expressions and conditional expressions in Scheme programming. Practice with hands-on exercises to solidify your understanding.

alann
Download Presentation

Scheme Comparing and Conditional Expressions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Section 4 Comparing and Conditional Expressions Version: 1.1.0 Prepared by: IT Group Last modified: Apr 16, 2008

  2. Contents • Comparing Expressions • Conditional Expressions • The Scholarship Problem • The Scholarship Solution • Exercises

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

  4. 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 y): ``x is equal to y''; • (< x y): ``x is strictly less than y''; and • (> x y): ``x is strictly greater than y''.

  5. 1.2.Result of a Comparing Expression • Result of a comparing expression is trueor false. • Examples:

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

  7. 1.4. Hands-On Exercises • 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)) • 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

  8. Conditional Expressions 2.1. Conditional Expressions in Scheme 2.2. Hands-On Exercises

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

  10. 2.2. Hands-On Exercises • 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?

  11. 3. Scholarship Problem • BlueSky school wants to give scholarship for their student. Scholarship will be awarded for following conditions: • With given GPA, design a program to calculate scholarship for a specific student.

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

  13. 4.1. Problem Description

  14. 4.2. Function Name

  15. 4.3. Test Cases

  16. 4.4. Function Template

  17. 4.5. Implementation

  18. 4.6. Testing

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

  20. 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:

  21. BMI categories Write a program to determine the weight status of a person belong to his/her height and weight.

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

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

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

  25. 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:

  26. 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 roomtype. • For the first week (amount of rent days is less than 7), total costis: 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

  27. The price list is below: • 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

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

More Related