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.090, IAP 2005—Lecture 3 1MASSACHVSETTS INSTITVTE OF TECHNOLOGYDepartment of Electrical Engineering and Computer Science6.090—Building Programming ExperienceIAP 2005Lecture 3Scheme1. Special Forms(a) begin - (begin exps)Evaluate each expression in order and return the value of the last expression.Problems1. Write abs, a function that returns the absolute value of its input.(abs 5);Value: 5(abs -5);Value: 5(define abs(lambda (val)2. Write sum-to-n which sums the numbers from 1 to n inclusive.(define sum-to-n(lambda (n)Alter the procedure to sum the squares of the numbers.6.090, IAP 2005—Lecture 3 23. Write a procedure that computes e.4. Write a procedure that runs forever. (Remember that C-c, C-c stops evaluation)5. Using string-append, write a procedure pad, which takes a string and a number, that returnsthe string with that number of spaces added to the end.(pad "yay" 0);Value: "yay"(pad "yay" 1);Value: "yay "(pad "yay" 3);Value: "yay "(define pad6. Write a procedure that uses Euclid’s algorithm to compute the GCD of two numbers. Euclid’salgorithm (according to Knuth it’s the oldest known algorithm) goes as follows: if r is theremainder of a divided by b, then the common divisors of a and b are the same as those of band r. Additionally, the gcd of a number and 0 is the number.(gcd 206 40);Value: 2(define gcd(lambda (a b)6.090, IAP 2005—Lecture 3 3Tower of HanoiScheme1. Special Forms(a) define (sugared form) - (define (name parameters) expressions)This form is equivalent to (define name (lambda (parameters) expressions)).(b) let - (let bindings body)Binds the given bindings for the duration of the body. The bindings is a list of (namevalue) pairs. The body consists of one or more expressions which are evaluated in orderand the value of last is returned.Problems7. Guess the value, then evaluate the expression in scheme. If your guess differs from the actualoutput, try desugaring any relevant expressions.(define (foo x)(+ x 3))foo(foo 5)(define bar 5)6.090, IAP 2005—Lecture 3 4(define (baz) 5)barbaz(bar)(baz)(let ((a 3)(b 5))(+ a b))(let ((+ *)(* +))(+ 3 (* 4 5)))(define m 3)(let ((m (+ m 1)))(+ m 1))(define n 4)(let ((n 12)(o (+ n 2)))(* n o))Guessing


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?