Scheme: Functions Chapter 3 of HTDP Ms. Knudtzon September 12.

Slides:



Advertisements
Similar presentations
Chapter 1 Applications and More Algebra.
Advertisements

PRICING WITH MARKET POWER II
Chapter 5 Mathematics of Finance.
Elasticity If a seller needs to reduce the price of a product, how much should it be reduced? Reduce too little, and projected increase in sales will not.
Analysis of Cost, Volume, and Pricing to Increase Profitability
Solving Quadratic Inequalities
Brought To You By- Tutorial Services-The Math Center
Copyright © Cengage Learning. All rights reserved.
Preview Warm Up California Standards Lesson Presentation.
Chapter 3 Mathematics of Finance
Chapter 1 Linear Equations and Graphs
Chapter 1 Applications and More Algebra.
Your boss asks… How many of these things do we have to sell before we start making money? Use your arrow keys to navigate the slides.
IENG313 Operation Research I
Chapter 18 Pricing Policies McGraw-Hill/Irwin
Copyright © Cengage Learning. All rights reserved.
Simple Interest Lesson
Copyright © Cengage Learning. All rights reserved.
Chapter 2 Section 3.
3.1 Solving Linear Equations Part I
4. Inequalities. 4.1 Solving Linear Inequalities Problem Basic fee: $20 Basic fee: $20 Per minute: 5¢ Per minute: 5¢ Budget: $40 Budget: $40 How many.
Transparency 7 Click the mouse button or press the Space Bar to display the answers.
U1A L6 Linear, Quadratic & Polynomial Inequalities
FIRM BEHAVIOR AND THE ORGANIZATION OF INDUSTRY
Copyright©2004 South-Western 13 The Costs of Production.
Key Concepts and Skills
Do now – grab a calculator
Section 4.3: Conditions and Conditional Functions.
CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Chapter 3: Programs are Functions Plus Variable Definitions Prof. Adam M. Wittenstein
Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Chapter 6 Ratio, Proportion, and Percent.
Exam 3 Material Formulas, Proportions, Linear Inequalities
Economics Chapter Supply, Demand, and Elasticity Combined Version
College Algebra Fifth Edition James Stewart Lothar Redlin Saleem Watson.
Thinking Mathematically Chapter 8 Consumer Math. Thinking Mathematically Section 1 Percent.
Math 34A Final Review Prepared by Vince Zaccone
6-0 Week 3 Lecture 3 Ross, Westerfield and Jordan 7e Chapter 6 Discounted Cash Flow Valuation.
Do Now 4/12/11 Take out HW from last night. Take out HW from last night.  Text p. 365, #8-16 evens and #15 Copy HW in your planner. Copy HW in your planner.
Jeopardy Formulas Perfect Competition Monopoly Price Discrimination Problems Q $100 Q $200 Q $300 Q $400 Q $500 Q $100 Q $200 Q $300 Q $400 Q $500 Final.
Lesson Quiz Lesson Quiz Lesson Presentation Lesson Presentation Lesson 3.6 Solving Multi-Step Inequalities Skill Check Skill Check.
5.3 Solve Multi-Step Inequalities You will solve multi- step inequalities. Essential question: How do you solve multi-step inequalities? You will learn.
Algebra Form and Function by McCallum Connally Hughes-Hallett et al. Copyright 2010 by John Wiley & Sons. All rights reserved. 3.1 Solving Equations Section.
Compound Inequalities Chapter 4.8 Part 1. Definition Compound Inequalities are two inequalities joined by the words “and” or “or”.
Objectives: To solve and graph simple and compound inequalities.
Solving Equations with Fractions. 2 Example: Solve for a. The LCD is 4. Simplify. Add 2a to both sides. Divide both sides by 3. Check your answer in the.
College Algebra Sixth Edition James Stewart Lothar Redlin Saleem Watson.
Chapter 7 Systems of Equations and Inequalities Copyright © 2014, 2010, 2007 Pearson Education, Inc Systems of Linear Equations in Two Variables.
Objective : Solving systems of linear equations by graphing System of linear equation two or more linear equations How do I solve linear systems of equations?
Inequalities Denoting Inequalities Properties Solving Inequalities.
Do Now According to some reports, supermarkets make a profit of three to six cents for every dollar of revenue. Where does the rest of the money go????
Copyright © 2010 Pearson Education, Inc. All rights reserved Sec
Copyright © Cengage Learning. All rights reserved. 2 Polynomial and Rational Functions.
$100 $200 $300 $400 $500 $200 $300 $400 $500 Problem Solving Multi-Step Equations Simple and Compound Interest Multi-Step Inequalities Transforming.
Functional Programming Language 1 Scheme Language: part 2.
Chapter 2 Inequalities. Lesson 2-1 Graphing and Writing Inequalities INEQUALITY – a statement that two quantities are not equal. SOLUTION OF AN INEQUALITY.
1 Copyright © Cengage Learning. All rights reserved. 2. Equations and Inequalities 2.2 Applied Problems.
Inequalities SPI Given a set of values, identify those that make an inequality a true statement. GLE Solve single- step linear equations.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
LAB: Inequalities with Negative Coefficients p.304 Q U E ST ION: How do you solve an inequality with a negative coefficient?
Chapter 2 Lesson 4 Solutions to Linear Inequalities.
1.7 – Day 1 Inequalities. 2 Objectives ► Solving Linear Inequalities ► Solving Nonlinear Inequalities ► Absolute Value Inequalities ► Modeling with Inequalities.
WARM UP Solve: 1. 3x – 5 = (3x -5) = x – 3 + 4x = (2x – 4) = 6.
Precalculus Fifth Edition Mathematics for Calculus James Stewart Lothar Redlin Saleem Watson.
Section 03 Programs are functions plus variable definitions Prepared by: IT Group Last modified: Apr 07, 2009.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
Solving Equations with the Variable on Each Side
Definition of a Polynomial Inequality
Linear Inequalities and Absolute Value
Quadratic Equations and Functions
Presentation transcript:

Scheme: Functions Chapter 3 of HTDP Ms. Knudtzon September 12

Check Point Lab Exercises from last week Fabric Exercises from the weekend

Scheme Programs Weve now written several simple functions in Scheme A program in Scheme usually consists of several inter-related functions In the area-of-ring function from last week, we used a small auxiliary function, area-of-disk that we had written in a previous step For very large programs, having auxiliary functions becomes more and more necessary

Class Problem Imagine the owner of a movie theater who has complete freedom in setting ticket prices. The more he charges, the fewer the people who can afford tickets. In a recent experiment the owner determined a precise relationship between the price of a ticket and average attendance. At a price of $5.00 per ticket, 120 people attend a performance. Decreasing the price by a dime ($.10) increases attendance by 15. Unfortunately, the increased attendance also comes at an increased cost. Every performance costs the owner $180. Each attendee costs another four cents ($0.04). The owner would like to know the exact relationship between profit and ticket price so that he can determine the price at which he can make the highest profit. How do we go about solving this problem? Need to figure out what the problem wants, and then write contracts, purpose and headers for each function.

Dependencies Profit is difference between revenue and cost Revenue is generated by sale of tickets. (Product of ticket price and # of attendees) Cost consists of two part - fixed part $180 and variable part that depends on # of attendees Also know that # of attendees depends on ticket price.

Definitions ;; profit : number -> number ;; to compute the profit as the difference between revenue and costs ;; at some given ticket-price (define (profit ticket-price)...) ;; revenue : number -> number ;; to compute the revenue, given ticket-price (define (revenue ticket-price)...) ;; cost : number -> number ;; to compute the costs, given ticket-price (define (cost ticket-price)...) ;; attendees : number -> number ;; to compute the number of attendees, given ticket-price (define (attendees ticket-price)...)

Examples Exercise The next step is to make up examples for each of the functions. Determine how many attendees can afford a show at a ticket price of $3.00, $4.00, and $5.00. Use the examples to formulate a general rule that shows how to compute the number of attendees from the ticket price. Exercise Use the results of exercise to determine how much it costs to run a show at $3.00, $4.00, and $5.00. Also determine how much revenue each show produces at those prices. Finally, figure out how much profit the monopolistic movie owner can make with each show. Which is the best price (of these three) for maximizing the profit?

Two ways ;; How to design a program (define (profit ticket-price) (- (revenue ticket-price) (cost ticket-price))) (define (revenue ticket-price) (* (attendees ticket-price) ticket- price)) (define (cost ticket-price) (+ 180 (*.04 (attendees ticket-price)))) (define (attendees ticket-price) (+ 120 (* (/ 15.10) ( ticket-price)))) ;; How not to design a program (define (profit price) (- (* (+ 120 (* (/ 15.10) ( price))) price) (+ 180 (*.04 (+ 120 (* (/ 15.10) ( price)))))))

Guidelines Formulate auxiliary function definitions for every dependency between quantities mentioned in the problem statement or discovered with example calculations. Give names to frequently used constants and use the names instead of the constants in programs. Variable definitions (define pi 3.14) (The shortcuts I told you about last week)

Homework Exercise 3.31 of HTDP (unit conversion)

Scheme: Conditionals Chapter 4 of HTDP Ms. Knudtzon September 13

Check Point 80 point quiz Fabric Exercise Homework Show me it is working (show off the 3 clothing items you designed in part 6) Show me your comments, test cases, etc Grades: 100, 80, 60

Booleans True or False values We already saw how to code equalities and inequalities in Scheme: (= x 5) (< x y) (>= x z) => These are called relational operations

Compound Conditions If we want two conditions to be true, use the and operator (and (= x y) (< y z)) If we want at least one of the conditions to be true, use the or operator (or (= x y) (< y z)) If we want the negation to be true, use the not operator (not (= x y))

Example Using Scheme to test a solution for polynomial ; equation1: number --> boolean ; to determine whether x is a solution for x^2 + 2x + 1 = 0 (define (equation1 x) (= (+ (* x x) (+ (* 2 x) 1)) 0)) ;Test Cases (equation1 -1) should be true (equation1 1) should be false Why did I put the zero on a separate line?

Symbols A symbol in Scheme is a set of characters preceded by a single quotation mark (no spaces allowed within symbol) Examples: dogeastchocolatehello WakeUp

Conditional Functions Sometimes what we want our program to do depends on certain conditions Is it raining? YesNo Take an Umbrella Bring a swimsuit (= x 0) YesNo Say Cant divide by zero (/ y x)

Conditionals in Scheme (cond [question answer] … [question answer]) (cond [question answer] … [else answer]) Arbitrary # of cond-lines Each cond-line has a condition (question) and a result (answer)

Example Conditional ;sign: number --> symbol ; consume number and returns whether num is ; zero, pos, or neg (define (sign anum) (cond [ (= anum 0) zero] ; question answer [ (> anum 0) positive] [ (< anum 0) negative] ) ) ; Test Cases (sign0) should be zero (sign 40) should be positive (sign -222) should be negative

Interest Rate Some banks pay different levels of interest for saving accounts. The more a customer deposits, the more the bank pays. In such arrangements, the interest rate depends on the interval into which the savings amount falls. To assist their bank clerks, banks use interest-rate functions. An interest function consumes the amount that a customer wishes to deposit and responds with the interest that the customer receives for this amount of money.Suppose the bank pays 4% for deposits of up to $1,000 (inclusive), 4.5% for deposits of up to $5,000 (inclusive), and 5% for deposits of more than $5,000. Write the interest-rate function for the bank.

Writing Conditionals Start by sketching the outline of a conditional expression (for the right number of questions) Then fill in the questions (cond [(<= amount 1000) …] [(<= amount 5000) …] [(> amount 5000) …]) (cond [() …] [() …]) Then fill in the answers (cond [(<= amount 1000).040] [(<= amount 5000).045] [(> amount 5000).050])

Data Analysis Data analysis - understand the different situations that the problem statement discusses Enumerate all possible situations Adds a step to the design recipe

Check your understanding Develop the function interest. Like interest-rate, it consumes a deposit amount. Instead of the rate, it produces the actual amount of interest that the money earns in a year. The bank pays a flat 4% for deposits of up to $1,000, a flat 4.5% per year for deposits of up to $5,000, and a flat 5% for deposits of more than $5,000.

Wednesday & Thursday Lab Days

Scheme: Symbols Chapter 5 of HTDP Ms. Knudtzon September 16

Checkpoint Lab/Homework exercises on conditionals Show me structure of code and test cases working 80 point quiz #2

Symbols Operations Scheme has one basic operation for symbols, a comparison operator which returns true if the two symbols are identical The operator is: symbol=? To use it: (symbol=? Hello Hello) (symbol=? hello howdy) (define x hello) (symbol=? hello x) Note: It is case-sensitive!

Strings As some of you have found from experimentation, Scheme also provides strings, which must be enclosed in double quotes. There is also a String comparison operator, string=? It is also case-sensitive

Symbols/Strings Historical Note: Symbols were first introduced by researchers in AI who wanted to design functions that could have conversations with people For now, assume symbols & strings to be fairly interchangeable It mostly depends if you need spaces or not Later, we will learn more about the differences between strings & symbols

Using Strings We could write a function that took in a greeting and made a response ;; reply: string --> string ;; to determine a reply for the greeting s (define (reply s) (cond [ (string=? s Good Morning) Hi ] [ (string=? s How are you?) Fine ] [ (string=? s Good Afternoon) I need a nap ] [ (string=? s Good Evening) Good night ])) ;Test (reply Good Morning) should be Hi

Checking Input What if you wanted to check if the input you got was a string? Or if it was a number? Or if it was a symbol? Scheme has operators to do just that. Each of the following operators return a boolean (string? X) (symbol? X) (number? X)

Exercises Homework - On Website In Class: Exercise Develop the function check-guess. It consumes two numbers, guess and target. Depending on how guess relates to target, the function produces one of the following three answers: 'TooSmall, 'Perfect, or 'TooLarge. The function implements one part of a two-player number guessing game. One player picks a random number between 0 and The other player's goal is to determine this number, called target, with the least number of guesses. To each guess, the first player responds with one of the three responses that check-guess implements. The function check-guess and the teachpack guess.ss implement the first player. The teachpack picks the random number, pops up a window in which the second player can choose digits, and hands over the guess and the target to check-guess. To play the game, set the teachpack to guess.ss using the Language|Set teachpack option. Then evaluate the expression (guess-with-gui check-guess) After check-guess has been thoroughly tested