This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

MASSACHVSETTS INSTITVTE OF TECHNOLOGYDepartment of Electrical Engineering and Computer Science6.001—Structure and Interpretation of Computer ProgramsFall 2007Recitation 2More SchemeScheme1. Basic Elements(a) self-evaluating - expressions whose value is the same as the expression.(b) names - Name is looked up in the symbol table to find the value associated with it.Names may be made of any collection of characters that doesn’t start with a number.2. Combination( procedure arguments-separated-by-spaces )Value is determined by evaluating the expression for the procedure and applying the resultingvalue to the value of the arguments.3. Special Forms(a) define - (define name value)The name is bound to the result of evaluating the the value. Return value is unspecified.1(b) if - (if test consequent alternative)If the value of the test is not false (#f), evaluate the consequent, otherwise evaluate thealternative.(c) lambda - (lambda (arg1 . . . argn) expression1 . . . expressionn)We will see this in more detail in lecture. A lambda captures a common pattern ofcomputation as a procedured. When applied to a set of arguments, it “substitutes” thevalue of each expression for the corresponding argument in the body of the lambda, thenevaluates the body.Problems1. Evaluate the following expressions4(+ 1 2)(7)(* (+ 7 8) ( - 5 6))(define one 1)(define two (+ 1 one))(define five 3)(+ five two)(define biggie-size *)(define hamburger 1)2(biggie-size hamburger five)(= 7 (+ 3 4))(= #t #f)((+ 5 6))biggie-size2. Evaluate the following expressions (assuming x is bound to 3):(if #t (+ 1 1) 17)(if #f #f 42)(if (> x 0) x (- x))(if 0 1 2)(if x 7 (7))3. Evaluate the following expressions:(lambda (x) x)((lambda (x) x) 17)((lambda (x y) x) 42 17)((lambda (x y) y) (/ 1 0) 3)((lambda (x y) (x y 3)) (lambda (a b) (+ a b)) 14)4. Suppose we’re designing an point-of-sale and order-tracking system for Wendy’s1. Luckilythe¨Uber-Qwuick drive through supports only 4 options: Classic Single Combo (hamburgerwith one patty), Classic Double With Cheese Combo (2 patties), and Classic Triple withCheese Combo (3 patties), Avant-Garde Quadruple with Guacamole Combo (4 patties). Weshall encode these combos as 1, 2, 3, and 4 respectively. Each meal can be biggie-sized toacquire a larger box of fries and drink. A biggie-sized combo is represented by 5, 6, 7, and 8respectively.(a) Write a procedure named biggie-size which when given a regular combo returns abiggie-sized version.16.001 and MIT do not endorse and are not affiliated with Wendy’s in any way. They merely capitalize on thepleasant way “biggie-size” rolls off the tongue.3(b) Write a procedure named unbiggie-size which when given a biggie-sized combo returnsa non-biggie-sized version.(c) Write a procedure named biggie-size? which when given a combo, returns true if thecombo has been biggie-sized and false otherwise.(d) Write a procedure named combo-price which takes a combo and returns the price ofthe combo. Each patty costs $1.17, and a biggie-sized version costs $.50 extra overall.(e) An order is a collection of combos. We’l encode an order as each digit representing acombo. For example, the order 237 represents a Double, Triple, and biggie-sized Triple.Write a procedure named empty-order which takes no arguments and returns an emptyorder.(f) Write a procedure named add-to-order which takes an order and a combo and returns anew order which contains the contents of the old order and the new combo. For example,(add-to-order 1 2) -> 12.(g) Write a procedure named order-size which takes an order and returns the number ofcombos in the order. For example, (order-size 237) -> 3. You may find quotient(integer division) useful.(h) Write a procedure named order-cost which takes an order and returns the total cost ofall the combos. In addition to quotient, you may find remainder (computes remainderof division)


View Full Document

MIT 6 001 - Lecture Notes

Documents in this Course
Quiz 1

Quiz 1

6 pages

Databases

Databases

12 pages

rec20

rec20

2 pages

Quiz II

Quiz II

15 pages

Streams

Streams

5 pages

Load more
Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?