Lab 2:


"I lambda Scheme"

This week we will learn a new special form, lambda, that can make procedures! Make sure you learn it well. It will be used extensively for the rest of this course.


finish this during section

Exercise 1.

Type each of the following into Scheme, and note the results. See if you can predict the results before letting Scheme do the computation

(lambda (x) (+ x 3))
((lambda (x) (+ x 3)) 7)

You can think of lambda as meaning "the function of...," e.g., "the function of x that returns (+ x 3)."

(define (make-adder num) 
  (lambda (x) (+ x num))) 
((make-adder 3) 7) 
(define plus3 (make-adder 3)) 
(plus3 7) 
(define (square x) (* x x)) 
(square 5) 
(define sq (lambda (x) (* x x))) 
(sq 5) 
(define (try f) (f 3 5)) 
(try +) 
(try word)

Exercise 2.


Write a procedure substitute that takes three arguments: a sentence, an old word, and a new word. It should return a copy of the sentence, but with every occurrence of the old word replaced by the new word.

Exercise 3.

Mystery Function g

Consider a Scheme function g for which the expression

((g) 1) 

returns the value 3 when evaluated.

Determine how many arguments g has. In one word, also describe as best you can the type of value returned by g.

Exercise 4.

Mystery Function f

For each of the following expressions, what must f be in order for the evaluation of the expression to succeed, without causing an error? For each expression, give a definition of f such that evaluating the expression will not cause an error, and say what the expression's value will be, given your definition.

Exercise 5.

Be the Interpreter.

Find the values of the following expressions where 1+ is a primitive procedure that adds one to its argument, and t is defined as follows:

(define (t f) 
  (lambda (x) (f (f (f x)))) ) 

Exercise 6.

Be the Interpreter.

Find the values of the following expressions where t is defined as in question 5 above, and s is defined as follows:

(define (s x) 
  (+ 1 x))

Exercise 7.

Make Tester

Write and test the make-tester procedure. Given a word w as argument, make-tester returns a procedure of one argument x that returns true if x is equal to w and false otherwise.


do this in section if possible; finish the rest at home

Exercise 8.

1. Abelson & Sussman, exercises 1.31(a), 1.32(a), 1.33, 1.40, 1.41, 1.43, 1.46

(Pay attention to footnote 51; you'll need to know the ideas in these exercises later in the semester.)


For 1.31a, you should base your product function off of the sum function earlier in the text. It should take 4 arguments - term, a, next, and b. Find the sum function and figure out what each of these arguments do.

For 1.31a, the function to estimate pi should be called estimate-pi. It should take in no arguments, and should estimate pi using at least 100 terms of the formula given in SICP.

For 1.33, the predicate should be the last argument to filtered-accumulate (see text box).

For 1.33, you should define functions sum-sq-prime and prod-of-some-numbers (see text box).

Answer the book questions below.

Exercise 9.


Last week you wrote procedures squares, that squared each number in its argument sentence, and saw pigl-sent, that pigled each word in its argument sentence. Generalize this pattern to create a higher-order procedure called every that applies an arbitrary procedure, given as an argument, to each word of an argument sentence.

Exercise 10.

Using Higher Order Fns

Our Scheme library provides versions of the every function from the last exercise and the keep function shown in lecture. Get familiar with these by trying examples such as the following:

Exercise 11.

Do the following reading: