Presentation is loading. Please wait.

Presentation is loading. Please wait.

Section 1.1: Flying a UFO (continued). Conversion recipe 1)Expand abbreviations 2)Parenthesize all subexpressions that have an operator. Should end up.

Similar presentations


Presentation on theme: "Section 1.1: Flying a UFO (continued). Conversion recipe 1)Expand abbreviations 2)Parenthesize all subexpressions that have an operator. Should end up."— Presentation transcript:

1 Section 1.1: Flying a UFO (continued)

2 Conversion recipe 1)Expand abbreviations 2)Parenthesize all subexpressions that have an operator. Should end up w/same number of operators, left-parentheses, and right-parentheses. 3)Move each operator to just after its own left parenthesis. (operation BEFORE operands) 3z becomes 3 * z z 2 becomes z * z 3*3+4*5 becomes ((3*3)+(4*5)) (-b+√(b 2 - 4ac))/2a becomes (((-b)+sqrt((b*b)-(4*a*c)))/(2*a))

3 Back to the UFO example… We know that the formula for the X-coordinate is: X = 10 * t + 20 How do we make this into a program? First, lets name the program X. Then, we need to write the expression 10 * t + 20. Use the conversion recipe.

4 Using the Conversion Recipe Step 1: No abbreviations. Step 2: Each operation (* and +) gets its own set of parentheses. ( ( 10 * t ) + 20 ) Step 3: Operations BEFORE operands. a) Move *( ( * 10 t ) + 20 ) b) Move +( + ( * 10 t ) 20 )

5 Math grammar can be confusing… 1)Some operators go between two operands; others go before one operand; some require parentheses around operand, some don’t. 2)Need PEMDAS to resolve ambiguity. 3)Sometimes there's no visible operator; defaults to multiplication. 4)(3+4) means the same as 3+4, or even ((3+4)).

6 …Scheme has a simpler grammar 1)All operators go before however many operands they need. 2)All subexpressions must have parentheses around them (including the operator). 3)No hidden operators; if you mean *, say it. 4)No extra parentheses allowed; exactly one pair of parentheses per operator.

7 Structure of a Scheme header The header is the first line of the program. It includes: The word define, which is a big marker. It says that we are defining a function, which is the real name for such algebraic formulas. The next part, (X t), which tells us two things: -the name of the expression (X in this case) and -the name of the parameter(s), which are the variables on which a program depends (t in this case)

8 Structure of a Scheme program A program has two parts: –the header define(define expression name(X name(s) of variable(s)t) –the body the actual expression(+ (* 10 t) 20)) It is very important to make sure the parentheses match: that is, you need the same number of open parentheses and closed parenthesis.

9 Math vs. Scheme Here is the math way to find the x- coordinate: X = 10 · t + 20 And here is the Scheme way: (define (X t) (+ (* 10 t) 20))

10 Using a Scheme program Now that we have defined the function in Scheme, we must learn to use it. As we just discussed, the definition tells us that X depends on t. This means that when we want to use X: - we must supply a value for t Then -the Scheme program replies with the value of X. For example, typing (X 0) gives the value of X when t = 0. What is that value?

11 How does Scheme get 20? DrScheme, which is basically a calculator for Scheme functions, then evaluates this expression for us, using the rules from algebra that we know so well. 0 is one instance of the program. Scheme then finds out that in that instance, X = 20. To see how Scheme does this, we can use the Stepper.

12 Using the stepper DrScheme provides a Stepper tool that shows how a complex expression is evaluated, step-by-step. For example, take the expression (+ 2 (* 3 4)) Just as with PEMDAS, DrScheme must evaluate the (* 3 4) expression first to get 12. Then, it can evaluate (+ 2 12) to get 14.

13 Using the stepper To use the Stepper tool, put the expression(s) you want to evaluate into DrScheme's top area, the definitions window, then click the Step button:

14 Using the stepper Click next and you will get the answer of 14.

15 Using the two windows The top area, the definitions window, is for defining new programs (like X and Y). The bottom area, the interactions window, is for using programs once they are defined. Fortunately, Dr. Scheme already has some programs written inside of it for us to use. Examples are +, -, *, and /. To use the + program, like when we find 3 + 4, type it into the interactions window the Scheme way: (+ 3 4). After hitting enter, you get 7.

16 We define programs in the Definitions Window. (define (X t) (+ (* 10 t) 20)) We then test them in the Interactions Window. > (X 0) 20 > (X 1) 30 Using the two windows

17 Exercise 1.1.3: “Y” Program The game program will need to do several things, including: The game program will need to do several things, including: - figure out the X-coordinate - figure out the Y-coordinate - displaying the UFO and shots on the screen We already have a program for X. We already have a program for X. Now, write a program for Y. (Hint: look back to the work you did in exercise 1.1.1) Now, write a program for Y. (Hint: look back to the work you did in exercise 1.1.1)

18 Next time… Drawing the UFO on a canvas Today we worked with numbers to determine the location of the UFO. However we are not yet able to actual see the UFO at its location. We will see how to do that next time.


Download ppt "Section 1.1: Flying a UFO (continued). Conversion recipe 1)Expand abbreviations 2)Parenthesize all subexpressions that have an operator. Should end up."

Similar presentations


Ads by Google