DOC PREVIEW
MIT 6 001 - Recitation 2: More Scheme

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:

6.001 Recitation 2: More SchemeRI: Gerald Dalley9 Feb 2007Announcements / Notes• Lecture 2, slide 29 has 4 missing parentheses.• Sugarless lambda – (a) keeps it clear that creat-ing a procedure and assigning it to a name aretwo distinct steps, and (b) often we don’t needa name – we’ll see many examples of this later.• (if (is-serious?(scheduling-problem? you))(email [email protected])(attend-section-we-assigned you))• First tutorial is Monday or Tuesday• Mid-semester recitation feedback• DrScheme: case sensitivity & rationality• InstaQuiz discussionThe lambda Special Form(lambda parameters body)Creates a pro c edure with the given parametersand b ody. parameters is a list of names of variables.body is one or more scheme expressions. When theprocedure is applied, the body expressions are evalu-ated in order and the value of the last one is returned.Evaluate the following expressions:( lamb da (x ) x)(( lam bd a ( x) x ) 17)(( lam bd a ( x y) x) 42 17)(( lam bd a ( x y) y) (/ 1 0) 3)(( lam bd a ( x y) (x y 3))( lamb da (a b) (+ a b )) 14)The if Special Form(if test consequent alternative)If the value of the test is not false (#f), evaluate theconsequent, otherwise evaluate the alternative.Why must this be a special form?Does if give us new functionality?Evaluate the following expressions (assuming x isbound to 3):( if # t (+ 1 1) 17)( if # f # f 42)( if ( > x 0) x (- x ))( if 0 1 2)( if x 7 ( 7))Write the body of the following procedure:;; If x is not the sa me as the ex pec ted;; value , some illeg al e xpr ess ion is;; e val uat ed .;;;; Hi nts : ( equ als ? x y ) can be u se d to test;; for eq uiv ale nce and ( not x ) fl ips tr ue /;; fa lse v alue s .( defi ne ( check x e xpect ed )1The cond Special Form(cond (test-expr1 expr ...)(test-expr2 expr ...)(else expr ...))Evaluation rules:1. Evaluate test-expr12. If the value is not false (#f), evaluate the rest of the associated expressions and return the last value.3. Otherwise, continue to the next test expression and repeat.4. If no test expressions are non-false, evaluate the else clause and return the value of the last expression,if an else clause exists.Why must this be a special form?Does cond give us new functionality?Evaluate the following expressions (assuming x is bound to 3):( cond ((= 1 x ) " one " )((= 2 x ) " two ")((= 3 x ) " th ree" ))( cond ((( l ambda (x ) (= 3 x )) x ) " three " )( else " not thr ee " ))( cond (( lambda (x ) (= 2 x )) " two " )( else " not two " ))Biggie Size!Supp ose we’re designing an point-of-sale and order-tracking system for Wendy’s1. Luck-ily the¨Uber-Qwuick drive through supports only 4 options: Classic Single Combo(hamburger with one patty), Classic Double With Cheese Combo (2 patties), and Clas-sic Triple with Cheese Combo (3 patties), Avant-Garde Quadruple with GuacamoleCombo (4 patties). We shall encode these combos as 1, 2, 3, and 4 respectively. Eachmeal can be biggie-sized to acquire a larger box of fries and drink. A biggie-sized combois represented by 5, 6, 7, and 8 respectively.1. Write a procedure named biggie-size which when given a regular combo returns a biggie-sized version.( d efi ne bi gg ie -s iz e2. Write a procedure named unbiggie-size which when given a biggie-sized combo returns a non-biggie-sized version.( d efi ne un big gi e- si ze3. Write a procedure named biggie-size? which when given a combo, returns true if the combo has beenbiggie-sized and false otherwise.( d efi ne bi gg ie -s iz e ?4. Write a procedure named combo-price which takes a combo and returns the price of the combo. Eachpatty costs $1.17, and a biggie-sized version costs $.50 extra overall.( d efi ne co mb o- pr ic e5. An order is a collection of combos. We’ll encode an order as each digit representing a combo. Forexample, the order 237 represents a Double, Triple, and biggie-sized Triple. Write a procedure namedempty-order which takes no arguments and returns an empty order.( d efi ne em pt y- or de r6. Write a procedure named add-to-order which takes an order and a combo and returns a new orderwhich contains the contents of the old order and the new combo. For example, (add-to-order 1 2)-> 12.( d efi ne ad d- to- or de r7. *Write a procedure named order-size which takes an order and returns the number of combos in theorder. For example, (order-size 237) -> 3. You may find quotient (integer division) useful.( d efi ne or de r- si ze8. *Write a procedure named order-cost which takes an order and returns the total cost of all the combos.In addition to quotient, you may find remainder (com putes remainder of division) useful.( d efi ne or de r- co


View Full Document

MIT 6 001 - Recitation 2: More Scheme

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 Recitation 2: More Scheme
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 Recitation 2: More Scheme 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 Recitation 2: More Scheme 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?