1 / 57

CS1321: Introduction to Programming

This lecture covers conditionals in programming and introduces the Webwork system for submitting homework. Learn how to navigate Webwork and use predicates to test conditions in Scheme.

lisamiller
Download Presentation

CS1321: Introduction to Programming

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. CS1321:Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

  2. Today’s Menu I. Administrivia A. TURN IN YOUR ASSIGNMENT B. Be Nice . . . To Rice C. Webwork…the adventure begins II. Introducing Conditionals and Cond III. Symbols

  3. Administrivia Homework 3 . . . is now due.

  4. Administrivia We work closely with the authors of your book. We just came back from a workshop with them. Do not abuse the textbook website. When they have a problem, we have a problem; when we have a problem, you have a problem. Do not harass them!!!!!!! 1. Some students have asked for access to the solutions. As the website clearly says, solutions are provided for instructors only. Further attempts to obtain answers will be dealt with harshly.

  5. Administrivia 2. Others have sent infantile gibberish via the website. You have to be kidding me!!!GT’s integrity suffers. Grow up. 3. Lastly, some students have attempted to sign up for the Teach-Scheme program, even though this program is for teachers of Scheme only. Don’t do this.

  6. Webwork Shortly, we will use the WebWork system to hand in homework. The following slides show you the basics of this system.

  7. A quick introduction to doing your homework

  8. Webwork The cs1321 web page has (or shortly will feature) a guide to WebWork. You are required to read this guide. The following slides show you how to navigate WebWork once you’ve logged in.

  9. Click on “Profile” Who’s my TA?

  10. This is your (possibly new) recitation room and TA

  11. Click here for recitation info How can I stalk my TA? Most underlined text in WebWork Links to useful information More useful information! If you click on your TA’s link you’ll get info about them…

  12. Everything you ever wanted to Know about your TA Are you stalking me?

  13. But what about the homework?How do I do the homework? Read the guide on the cs1321 webpage.

  14. Making the Right choices As fun as it is to rewrite mathematical functions into Scheme functions, it would be nice to be able to do something more…interesting. After all, computer programs aren’t built JUST on mathematical equations. They’re built on conditions.

  15. Conditions What kind of conditionals? • Is the fiery demon-monster in line with the • projectile fired from my BFG-3000? • Does the plane have enough fuel to be put • in a holding pattern over the airport? • Does my car have to increase the amount • of gas going to the engine to maintain my • cruising speed of 80 mph?

  16. Booleans, the building blocks of conditions Booleans are a new class of values (along with the numbers and symbols we’ve already seen). They represent truth values, and have two settings: False True &

  17. Let’s start with some old familiar faces >, <, =

  18. { 4 = 5 5 = 5 5 = 6 False True False resolves to X = Y { True False False 4 < 5 5 < 5 5 < 6 resolves to X < Y { 4 > 5 5 > 5 5 > 6 False True False resolves to X > Y

  19. Now in Scheme… { (= 4 5) (= 5 5) (= 5 6) false true false resolves to (= X Y) { true false false (< 4 5) (< 5 5) (< 5 6) resolves to (< X Y) { (> 4 5) (> 5 5) (> 5 6) false true false resolves to (> X Y)

  20. Linking Conditionals and Saying what we mean Sometimes we want to combine two or three conditions together. If it’s raining AND I have some money, I’ll drive down to the Regal and catch a flick If I have $30 OR someone buys it off my wishlist, I’m going to get the Clerks – Collector’s Edition DVD.

  21. And sometimes we want to negate our conditional: If I do NOT get a loan this semester, I’ll be eating lots of ramen noodles.

  22. Scheme Functions that do just that x needs to be between 5 and 10 x needs to be greater than 6 or y needs to be less than 30 x can’t be 3 (and (> x 5) (< x 10)) (or (> x 6) (< y 30)) (not (= x 3))

  23. Short-circuit evaluation Just because we have a whole bunch of conditions doesn’t mean we have to use them all. We know some basic things about “and” and “or”…

  24. Run these through the Stepper: (and (> 5 3) (< 3 5) (= 3 5)) (and (= 3 5) (> 5 3) (< 3 5)) (and (> 5 3) (< 3 5) (= 3 3)) (or (> 3 5) (< 5 3) (= 3 3)) (or (> 5 3) (< 3 5) (= 3 3))

  25. Functions that Test Conditions In our lingo, they’re called: Predicates

  26. Predicates True (as in Real) Predicates have the following properties: 1) They Always Resolve To true Or false. (no numbers, symbols, structures, strings, hash tables, vectors, function bodies, etc) 2) The Function Name Always Ends With A ?

  27. Examples: (number? <value>) is a built in predicate that tests whether or not <value> is a number (define (is-between-5-6? n) is a defined predicate (and (< 5 n) (< n 6))) that tests to see if n is bigger than 5 but less than 6. (define (is-bigger-than-z? x y z) another defined (or (> x z) (> y z))) function that tests if either x or y are bigger than z

  28. Using Your Predicates So how do you go about putting these conditional functions to good use? How do we put these things into bigger functions? We need some sort of conditional statement!

  29. Cond The built-in function “cond” allows us to create our conditional statement. It’s format is as follows: (cond (question answer) (question answer) … (else answer)) (cond (question answer) (question answer) … (question answer)) OR The version on the left is more explicit!

  30. Cond…continued “What does (question answer) mean?” “question” is where you insert a predicate, a function that evaluates to true or false. “answer” is where you state what you’d like to happen should your question resolve to TRUE

  31. “How many questions and answer pairs can I have?” As many as you need. “cond” recognizes anywhere from one pair to many pairs. “What’s the deal with ‘else’?” “else” is a built-in catch-all clause for your cond statement. “Will this affect my Design Recipe?” You betcha.

  32. Example • Let’s design a simple example that uses cond. • Suppose we have a function that determines the unit price of a item discounted in quantity. Given one or more DVDs, the price is: • buy 1 DVD for $25.00 • buy 3 DVDs for $20.00 each • buy 10 or more DVDs for $12.00 each

  33. 1 Analysis We begin with the Data Analysis section. We know from the cs1321 template that the section is called: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; <1> ;; Data Analysis & Definitions: ;; <not yet required> For now, we’ll do HALF of this--the data analysis. A data definition will come later.

  34. Analysis So what do we know about the data our function will use? ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; <1> ;; Data Analysis & Definitions: ;; The unit price of 1 DVD is 25 ;; In sets of 3 or more, the unit price is 20 ;; In sets of 10 or more, the unit price is 12 We give a short description of the data we’re working with. Here unit price varies with the quantity of the order. A number!

  35. 2 Contract This gives us enough to build a contract ;; Contract: unit-price : Number --> Number We have to give the function a quantity, and it returns the unit price.

  36. 3 Purpose This leads us to the purpose ;; Purpose: to return the unit price of a ;; quantity of DVDs

  37. 3 10 4 Examples Now things get interesting... ;; Examples: ;; (unit-price 1) should return 25 ;; (unit-price 3) should return 20 ;; (unit-price 99) should return 12 ;; (unit-price 8) should return 20 Note that we’ve designed a test case for each condition:

  38. 3 10 What about zero units? It does not fit the problem statement. Examples Now things get interesting... ;; Examples: ;; (unit-price 1) should return 25 ;; (unit-price 3) should return 20 ;; (unit-price 99) should return 12 ;; (unit-price 8) should return 20 Note that we’ve designed a test case for each condition:

  39. 5 Template Our programs are not yet complicated enough for a template. ;; Template ;; < not required >

  40. 6 Definition Our definition falls out of the Data Analysis ;; Definition (define (unit-price quantity) (cond [ . . . ? . . . ]yipes! How many different conditions do I need?? [ . . . ? . . . ] )) Note that the ‘else’ could be rewritten with a specific case. This might make it easier to later modify the function for new prices/quantity discounts, etc.

  41. 6 Definition Our definition falls out of the Data Analysis ;; Definition (define (unit-price quantity) (cond [ . . . ? . . . ] look at your data analysis – it tells you!  [ . . . ? . . . ] )) Note that the ‘else’ could be rewritten with a specific case. This might make it easier to later modify the function for new prices/quantity discounts, etc.

  42. 6 Definition Our definition falls out of the Data Analysis ;; Definition (define (unit-price quantity) (cond [(< quantity 3) 25 ] [(and(>= quantity 3)(< quantity 10)) 20 ] [(>= quantity 10) 12 ] )) Note that we use three very specific exact cases. We’ve been careful to precisely cover the range of possibilities. This might make it easier to later modify the function for new prices/quantity discounts, etc.

  43. 6 Definition Our definition falls out of the Data Analysis ;; Definition (define (unit-price quantity) (cond [(< quantity 3) 25 ] [(and(>= quantity 3)(< quantity 10)) 20 ] [else 12 ] )) Note the ‘else’ being used this time. This form is not quite as explicit as the previous.

  44. 7 Tests ;; Tests (unit-price 1) ;; should return 25 (unit-price 3) ;; should return 20 (unit-price 99) ;; should return 12 (unit-price 8) ;; should return 20 We include test for each of our examples, and perhaps a few more...

  45. New Improved Tests ;; Tests (unit-price 1) = 25 (unit-price 3) = 20 (unit-price 99) = 12 (unit-price 8) = 20 We can also opt for the more cosmopolitan ‘=‘ approach instead of commented “should return”

  46. Tests But we’ve also learned about booleans today, and can also insert a few tests instead of numbers. ;; Tests (= (unit-price 1) 25) (= (unit-price 3) 20) (= (unit-price 99) 12) (= (unit-price 8) 20)

  47. Tests We can then see if our tests all return “true”. This lets the computer do the tedious chore checking if numbers are equal.

  48. Symbols Thus far, we’ve done only numeric processing. Let’s revisit the various elements of Scheme to learn more about symbolic information. Recall:

  49. Symbols A symbol is a sequence of characters preceded by a single forward quotation mark: ‘this ‘is ‘a ‘bunch ‘of ‘symbols By themselves, symbols have no meaning. It’s up to programs and users to give them meaning.

  50. Symbols Like numbers, symbols are atomic pieces of data. Even if they are made up of many letters, the symbol, as a whole is one thing. You can check to see if symbols are equal by using the symbol=? Predicate: (symbol=? ‘one ‘one) ==> true (symbol=? ‘one ‘two) ==> false

More Related