SICP 1.3.2: “Formulating Abstractions with Higher-Order Procedures: Constructing Procedures Using Lambda”

(Structure and Interpretation of Computer Programs) 1.3.2

Paige Finkelstein
2 min readSep 4, 2017

My 1.3.2 exercise solutions are also on Github here: https://github.com/bolducp/SICP/tree/master/exercises/chapter_01/1.3_exercises

image credit: https://building.coursera.org/img/reinvent_launch-page_illustration_lambda.png

As the title of this sub-section states, the focus of this material is on explaining the use of lambda in writing Scheme procedures. More specifically, this section defines and illuminates the usefulness of lambda functions by drawing upon previous examples where internal named procedures were defined in order only to be immediately invoked once within the body of the primary function definition and shows, instead, how lambda functions can be used to accomplish the same end more concisely and clearly. It also introduces the concept of local variable assignment via the keyword let and demonstrates how such variable assignment is, in fact, built on (just syntactic sugar for) a lambda function itself.

I’m familiar with lambda functions from Python and Elixir, and I use anonymous functions increasingly often in my Javascript code. But seeing the examples of how lambda functions could be used to improve the Scheme solutions I’ve written for previous exercises was a useful context to be re-introduced to the value of lambda functions. And seeing how Scheme uses lambda functions to enable local variable assignment is especially interesting in light of everything that I’ve learned about functional programming and immutability through my studies of Elixir.

Exercise 1.34

Suppose we define the procedure

(define (f g)(g 2))

Then we have (f square) = 4; (f (lambda (z) (* z (+ z 1)))) = 6

What happens if we (perversely) ask the interpreter to evaluate the combination (f f)? Explain.

The interpreter raises an error saying that 'The object 2 is not applicable,' because it first invokes the argument 'f' function inside the body, resulting in (f 2), at this point, 'f' is invoked with the argument 2, resulting in (2 2). 
But the f function expects to be called with another function as its argument, not with a primitive type. So it throws an error because 2 isn't a procedure so (2 2) is impossible.
(define (f g)
(g 2))
(f f)= ((f g)
(g 2) 2)
= (f 2) ;tries to call function f with 2 as its argument and fails
(2 2)

--

--