6.001, Spring 2004—Recitation 2 1MASSACHVSETTS INSTITVTE OF TECHNOLOGYDepartment of Electrical Engineering and Computer Science6.001—Structure and Interpretation of Computer ProgramsSpring 2004Recitation 2More SchemeScheme1. Special Forms(a) if - (if test consequent alternative)If the value of the test is not false (#f), evaluate the consequent, otherwise evaluate thealternative.(b) lambda - (lambda parameters body)Creates a procedure with the given parameters and body. Parameters is a list of namesof variables. Body is one or more scheme expressions. When the procedure is applied,the bo dy express ions are e valuated in order and the value of the last one is returned.Problems1. 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))6.001, Spring 2004—Recitation 2 22. 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)3. 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 Comb o (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.(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.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.6.001, Spring 2004—Recitation 2 3(e) An order is a collection of combos . We’l e ncode 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.(h) Write a procedure named order-cost which takes an order and returns the total costof all the combos. You may find quotient (integer division)
View Full Document