Presentation on theme: "Scheme: Functions Chapter 3 of HTDP Ms. Knudtzon September 12."— 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 3.1.1. 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 3.1.2. Use the results of exercise 3.1.1 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) (- 5.00 ticket-price)))) ;; How not to design a program (define (profit price) (- (* (+ 120 (* (/ 15.10) (- 5.00 price))) price) (+ 180 (*.04 (+ 120 (* (/ 15.10) (- 5.00 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.
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 5.1.2. 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 99999. 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
Your consent to our cookies if you continue to use this website.