CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester.

Slides:



Advertisements
Similar presentations
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Advertisements

CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Scheme: Functions Chapter 3 of HTDP Ms. Knudtzon September 12.
Introduction to Programming using Matlab Session 2 P DuffourJan 2008.
Exponential Functions Logarithmic Functions
 Take Roll  Discussion – BA 8  Questions?  Tips for revising the introduction  Workshop Time  Homework for next week.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
Mrs. Chapman. Tabs (Block Categories) Commands Available to use Script Area where you type your code Sprite Stage All sprites in this project.
Mr Barton’s Maths Notes
CSC 160 Computer Programming for Non-Majors Lecture #9: Booleans Prof. Adam M. Wittenstein
D. Roberts PHYS 121 University of Maryland PHYS 121: Fundamentals of Physics I September 1, 2006.
CS 106 Introduction to Computer Science I 03 / 30 / 2007 Instructor: Michael Eckmann.
Rewriting your function using map and foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.5 TexPoint fonts used in EMF. Read the TexPoint manual.
Function Composition CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons.
Today’s quiz on 8.2 A Graphing Worksheet 1 will be given at the end of class. You will have 12 minutes to complete this quiz, which will consist of one.
CSC 160 Computer Programming for Non-Majors Lecture #3a: Stepper, Words, Sentences Prof. Adam M. Wittenstein
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
Please open your laptops, log in to the MyMathLab course web site, and open Daily Quiz 16. IMPORTANT NOTE: If you have time left out of your five minutes.
Please open your laptops, log in to the MyMathLab course web site, and open Daily Quiz 18. You will have 10 minutes for today’s quiz. The second problem.
Lesson 4: Percentage of Amounts.
Time Management.
Mr. Wortzman. Tabs (Block Categories) Available Blocks Script Area Sprite Stage All sprites in this project.
Relations And Functions. Objective All students will be able to determine whether a relation is a function and identify the domain and range of a function.
CS CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 5 Sept 4th, 2001 Fall Semester.
Computer Science 1000 Spreadsheets II Permission to redistribute these slides is strictly prohibited without permission.
Any questions on today’s homework? (Sections 1.6/1.7) Reminder: You should be doing this homework without using a calculator, because calculators can’t.
CS 122 Engineering Computation Lab Lab 2 Dan De Sousa and Tim Cheeseman Department of Computer Science Drexel University April 2009 ©By the author. All.
How to use the internet The internet is a wide ranging network that thousands of people use everyday. It is a useful tool in modern society that once one.
Word Revise documents and keep track of changes. Use Track Changes and comments Course contents Overview: Insertions, deletions, comments Lesson 1: Stay.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Math 105: Problem Solving in Mathematics
Please get out your completed 8.2B Graphing Worksheet 2 and pass it to the center aisle to be collected by the TA.
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
1 Daily Announcements CS 202, Spring 2007 Aaron Bloomfield.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
1 CS1100 Fall Instructor: David Gries CS100M: Matlab No prior programming experience One semester of calculus Math & engineering type problems CS100J:
Rewriting your function using map and foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual.
Modifying HTML attributes and CSS values. Learning Objectives By the end of this lecture, you should be able to: – Select based on a class (as opposed.
Conditional Statements.  Quiz  Hand in your jQuery exercises from last lecture  They don't have to be 100% perfect to get full credit  They do have.
Any questions on today’s homework? (Sections 1.6/1.7) Reminder: You should be doing this homework without using a calculator, because calculators can’t.
 In this packet we will look at:  The meaning of acceleration  How acceleration is related to velocity and time  2 distinct types acceleration  A.
Sight Words.
Advanced Engineering Mathematics, 7 th Edition Peter V. O’Neil © 2012 Cengage Learning Engineering. All Rights Reserved. CHAPTER 4 Series Solutions.
Lecture on Set! And Local CS 2135 Copyright Kathi Fisler, 2002 This material requires Advanced Language Level.
ITP © Ron Poet Lecture 6 1 More on if. ITP © Ron Poet Lecture 6 2 Remembering Tests  We often want to remember the result of a test, so that we can use.
Word Create a basic TOC. Course contents Overview: table of contents basics Lesson 1: About tables of contents Lesson 2: Format your table of contents.
CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.
CS CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 6 Sept 6th, 2001 Fall Semester.
CS CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 13 October 4, 2001 Fall Semester.
Sometimes Structural Recursion Isn't Enough CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.1 TexPoint fonts used in EMF. Read the TexPoint manual.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
1 CS100J Spring Instructor: David Gries CS100M: Matlab No prior programming experience One semester of calculus Math & engineering type problems.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
This I Believe Essay Writer’s Workshop: Introductions, Juicy Details, & Conclusions 8 th ELA St. Joseph School.
BY: KAYLEE J. KAMRYN P. CLOE B. EXPRESSIONS * EQUATIONS * FUNCTIONS * AND INEQUALITIES.
Writing & Graphing Inequalities Learning Target: Today I am learning how to write and graph inequalities on the number line because I want to be able.
Loops Brent M. Dingle Texas A&M University Chapter 6 – Section 6.3 Multiway Branches (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
More about comments Review Single Line Comments The # sign is for comments. A comment is a line of text that Python won’t try to run as code. Its just.
Microsoft Word Reviewing Documents.
Rewriting your function using map and foldr
Presentation transcript:

CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

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

Administrivia Homework 3... is now due.

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.

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.

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

A quick introduction to doing your homework

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.

Who’s my TA? Click on “Profile”

This is your (possibly new) recitation room and TA

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

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

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

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.

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?

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: True & False

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

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

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

Quick scheme Demo

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.

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.

Scheme Functions that do just that (and (> x 5) (< x 10)) (or (> x 6) (< y 30)) (not (= x 3)) 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

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”…

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))

Functions that Test Conditions In our lingo, they’re called: 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 ?

Examples: (number? ) is a built in predicate that tests whether or not 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

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!

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

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

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

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

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

Analysis So what do we know about the data our function will use? ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 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!

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

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

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:

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: 3 10 What about zero units? It does not fit the problem statement.

Template Our programs are not yet complicated enough for a template. ;; Template ;; 5

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

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

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”

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

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

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

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.

Symbols (demonstration of valid/invalid symbols)

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

Symbols And just like numbers, we can return symbols, or pass them into a function. (define (day-of-week day-num) (cond [ (= day-num 0) ‘Saturday ] [ (= day-num 1) ‘Sunday ] [ (= day-num 2) ‘Monday ] [ (= day-num 3) ‘Tuesday ] [ (= day-num 4) ‘Wednesday ] [ (= day-num 5) ‘Thursday ] [ (= day-num 6) ‘Friday ] ))

Symbols What’s the contract for this one? (define (day-of-week day-num) (cond [ (= day-num 0) ‘Saturday ] [ (= day-num 1) ‘Sunday ] [ (= day-num 2) ‘Monday ] [ (= day-num 3) ‘Tuesday ] [ (= day-num 4) ‘Wednesday ] [ (= day-num 5) ‘Thursday ] [ (= day-num 6) ‘Friday ] ))

Symbols ;; Contract: day-of-week: number -> symbol (define (day-of-week day-num) (cond [ (= day-num 0) ‘Saturday ] [ (= day-num 1) ‘Sunday ] [ (= day-num 2) ‘Monday ] [ (= day-num 3) ‘Tuesday ] [ (= day-num 4) ‘Wednesday ] [ (= day-num 5) ‘Thursday ] [ (= day-num 6) ‘Friday ] ))

Strings – more later… In DrScheme, you can also define compound pieces of data called strings. Strings are not atoms of data like symbols--they are made of many, many parts. “this is a string” (define name “John Doe”) Like symbols, strings are symbolic data. But that’s where the similarity stops. We’ll use string later, once we come to understand compound data.

Wow! Images In DrScheme, you can also define images--a third type of symbolic data. Use the “Insert Image…” option to load an image.

Images – can you believe it? The images then become bound to the variable names we give them. We’ll work more with graphics later on.

Try it!