This preview shows page 1 out of 2 pages.

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

Unformatted text preview:

Worksheet #6, Michael Collins========================================================================Procedures which take procedures as arguments:Assume the following definition of sum(define sum (lambda (term-proc a next-proc b)(if (> a b)0(+ (term-proc a) (sum term-proc (next-proc a) next-proc b)))))Work out what happens when we evaluate(sum (lambda (x) (x)) 1 (lambda (x) (+ x 1)) 10)How do we compute the sum for i = 1 to 10 of 1/i?How do we compute the sum for i = {1, 3, 5, 7, 9} of (i^3 + 2i)?Now consider another definition of sum:(define sum (lambda (term-proc a next-proc condition)(if (condition a)0(+ (term-proc a) (sum term-proc (next-proc a) next-proc condition)))))How do we sum the integers between 1 and 10 inclusive using thisdefinition?========================================================================Procedures which return procedures:write a procedure (sum-func f g) which takes two procedures f(x) and g(x)and returns a new procedure which is f(x)+g(x)What is the type of this procedure?========================================================================Procedures which return procedures:write a procedure (max-func f g) which takes two procedures f(x) and g(x)and returns a new procedure h which is h(x) = max{f(x),g(x)}========================================================================Procedures which return procedures:Assume we have defined ‘‘compose’’ as follows(define compose (lambda (f g) (lambda (x) (f (g x)))))Now define a new procedure compose-n which composes a function withitself n times1========================================================================Assume the following definitions for ‘‘map’’, ‘‘accumulate’’ and ‘‘filter’’(define (map proc seq)(if (null? seq)nil(cons (proc (car seq))(map proc (cdr seq)))))(define (accumulate op init seq)(if (null? seq)init(op (car seq) (accumulate op init (cdr seq)))))(define (filter pred seq)(if (null? seq)nil(let ((rest (filter pred (cdr seq))))(if (pred (car seq))(cons (car seq) rest)rest))))What are the values for the following expressions?:(map (lambda (x) (- x 5)) (list 1 2 3 4))(accumulate * 1 (list 1 2 3 4))(filter (lambda (x) (< x 3)) (list 1 2 3 4))Now write functions using map, filter or accumulate which:1) calculates the sum of elements in a list, e.g. (list 1 2 3 4) => 102) calculates the length of a list========================================================================A final (tough) question: say we define ls to be a list of *procedures*:(define (square x) (* x x))(define (double x) (* x 2))(define (inc x) (+ x 1))(define ls (list square double inc))Now say we want a function ‘‘apply-procs’’ that behaves as follows:(apply-procs ls 4)=> ((square 4) (double 4) (inc 4)) = (16 8 5)(apply-procs ls 3)=> ((square 3) (double 3) (inc 3)) = (9 6 4)How do we achieve this using


View Full Document

MIT 6 001 - Worksheet 6

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 Worksheet 6
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 Worksheet 6 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 Worksheet 6 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?