DOC PREVIEW
MIT 6 001 - Problem Slides

This preview shows page 1-2-3-24-25-26-27-49-50-51 out of 51 pages.

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

Unformatted text preview:

Playing Board2003004005001002003004005001002003004005001002003004005002004006008001000100Folding Wrong with Filtered MapsDrawing SwordsLong Live fold-right!Substituting RegentsTIARA Is A Recursive AcronymSuppose x is bound to the list (1 2 3 4 5 6 7). Using map, filter, and/or fold-right, write an expression involving x that returns:(1 4 9 16 25 36 49)Suppose x is bound to the list (1 2 3 4 5 6 7). Using map, filter, and/or fold-right, write an expression involving x that returns:(1 4 9 16 25 36 49)(map (lambda (x) (* x x)) x)Suppose x is bound to the list (1 2 3 4 5 6 7). Using map, filter, and/or fold-right, write an expression involving x that returns:((1 1) (3 3) (5 5) (7 7))Suppose x is bound to the list (1 2 3 4 5 6 7). Using map, filter, and/or fold-right, write an expression involving x that returns:((1 1) (3 3) (5 5) (7 7))(map (lambda (x) (list x x))(filter odd? x))Suppose x is bound to the list (1 2 3 4 5 6 7). Using map, filter, and/or fold-right, write an expression involving x that returns:((2) ((4) ((6) #f)))Suppose x is bound to the list (1 2 3 4 5 6 7). Using map, filter, and/or fold-right, write an expression involving x that returns:((2) ((4) ((6) #f)))(fold-right (lambda (x accum) (cons (list x) (list accum)))#f(filter even? x))Suppose x is bound to the list (1 2 3 4 5 6 7). Using map, filter, and/or fold-right, write an expression involving x that returns:The maximum element of x: 7Suppose x is bound to the list (1 2 3 4 5 6 7). Using map, filter, and/or fold-right, write an expression involving x that returns:The maximum element of x: 7(fold-right max (car x) (cdr x))Suppose x is bound to the list (1 2 3 4 5 6 7). Using map, filter, and/or fold-right, write an expression involving x that returns:The last pair of x: (7)Suppose x is bound to the list (1 2 3 4 5 6 7). Using map, filter, and/or fold-right, write an expression involving x that returns:The last pair of x: (7)Answer: trick question! It’s not possible. Map, filter, and fold-right do not give you access to the original list’s backbone, they only let you see the values.Draw a box-and-pointer diagram for the value produced by the following expression: (cons (cons "x" nil) (cons "y" (cons "z" nil)))Draw a box-and-pointer diagram for the value produced by the following expression: (cons (cons "x" nil) (cons "y" (cons "z" nil)))“x”“y” “z”What code will produce the following box-and-pointer diagram?xWhat code will produce the following box-and-pointer diagram?(define null-null (cons '() '())(define x (cons (cons (cons '() null-null)'())null-null)xWrite code that will cause the following to be printed:(1 2 (3 (((4))) 5))Write code that will cause the following to be printed:(1 2 (3 (((4))) 5))(list 1 2 (list 3 (list (list (list 4)))5))(cons 1 (cons 2 (cons (cons 3 (cons (cons (cons (cons 4 '()) '()) '()) (cons 5 '()))) '())))Draw a box-and-pointer diagram for the value produced by the following expression: (map car (list (list 3) (list 4) (list 5)))Draw a box-and-pointer diagram for the value produced by the following expression: (map car (list (list 3) (list 4) (list 5)))3 4 5Draw the box-and-pointers diagram for the value of the following expression: (fold-right append '() (list (list "a" "b") (list "c") (list "d" "e" "f")))Draw the box-and-pointers diagram for the value of the following expression: (fold-right append '() (list (list "a" "b") (list "c") (list "d" "e" "f")))“a” “b” “c” “d” “e” “f”Write the following procedure using fold-right: ; Creates a new list with ; the same elements as lst(define (copy-list lst))Write the following procedure using fold-right: ; Creates a new list with ; the same elements as lst(define (copy-list lst)(fold-right cons '() lst))Write the following procedure using fold-right: (define (append list1 list2))Write the following procedure using fold-right: (define (append list1 list2)(fold-right cons list2 list1))Write a procedure to reverse a list using fold-right (you may also use length, append, list, and/or cons): (define (reverse lst))Write a procedure to reverse a list using fold-right (you may also use length, append, list, and/or cons): (define (reverse lst)(fold-right (lambda (new accum) (append accum(list new))) '() lst))Write the for-all? procedure using fold-right. It should return #t if applying the procedure pred to each element of lstevaluates to #t. ;; for-all? : ;; list<A>,(A->boolean) -> boolean;; Examples: ;; (for-all? (list 1 3 5 7) odd?) => #t;; (for-all? (list 1 3 5 6) odd?) => #f(define (for-all? lst pred) ... )Write the for-all? procedure using fold-right. It should return #t if applying the procedure pred to each element of lst evaluates to #t. ;; for-all? : ;; list<A>,(A->boolean) -> boolean;; Examples: ;; (for-all? (list 1 3 5 7) odd?) => #t;; (for-all? (list 1 3 5 6) odd?) => #f(define (for-all? lst pred) (fold-right (lambda (x accum) (and accum (pred x))) #t lst))Write the procedure map in terms of fold-right. (define (map pred lst) ... )Write the procedure map in terms of fold-right. (define (map pred lst) (fold-right (lambda (x accum) (cons (pred x) accum)) '() lst))Write the value of the final Scheme expression. Assume the expressions are evaluated in order. Use unspecified, error, or procedure where appropriate.. ((lambda (x) (+ x x)) 5)Write the value of the final Scheme expression. Assume the expressions are evaluated in order. Use unspecified, error, or procedure where appropriate.. ((lambda (x) (+ x x)) 5) => 10Write the value of the final Scheme expression. Assume the expressions are evaluated in order. Use unspecified, error, or procedure where appropriate.. (define x 5)(define y 6)(let ((x 7)(y x))(+ x y))Write the value of the final Scheme expression. Assume the expressions are evaluated in order. Use unspecified, error, or procedure where appropriate.. (define x 5)(define y 6)(let ((x 7)(y x))(+ x y)) => 12Write the value of the final Scheme expression. Assume the expressions are evaluated in order. Use unspecified, error, or procedure where appropriate.. (define x 10)(define y 20)(define (foo x) (lambda (y) (- x y)))((foo y) x)Write the value of the final Scheme expression. Assume the expressions are evaluated in order. Use unspecified, error, or procedure where appropriate.. (define x 10)(define y 20)(define (foo x) (lambda (y) (- x y)))((foo y) x) => 10Write the value of the final Scheme expression. Assume the expressions are evaluated in order. Use unspecified,


View Full Document

MIT 6 001 - Problem Slides

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 Problem Slides
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 Problem Slides 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 Problem Slides 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?