Printer-friendly versionPrinter-friendly version

In using sum as in section 1.3.1, it seems terribly awkward to have to define trivial procedures such as pi-term and pi-next just so we can use them as arguments to our higher-order procedure. Rather than define pi-next and pi-term, it would be more convenient to have a way to directly specify “the procedure that returns its input incremented by 4” and “the procedure that returns the reciprocal of its input times its input plus 2.” We can do this by introducing the special form lambda, which creates procedures. Using lambda we can describe what we want as

(lambda (x) (+ x 4))

and

(lambda (x) (/ 1.0 (* x (+ x 2))))

Then our pi-sum procedure can be expressed without defining any auxiliary procedures as

(define (pi-sum a b)
  (sum (lambda (x) (/ 1.0 (* x (+ x 2))))
       a
       (lambda (x) (+ x 4))
       b))

Again using lambda, we can write the integral procedure without having to define the auxiliary procedure add-dx:

(define (integral f a b dx)
  (* (sum f
          (+ a (/ dx 2.0))
          (lambda (x) (+ x dx))
          b)
     dx))

In general, lambda is used to create procedures in the same way as define, except that no name is specified for the procedure:

(lambda (<formal-parameters>) <body>)

The resulting procedure is just as much a procedure as one that is created using define. The only difference is that it has not been associated with any name in the environment. In fact,

(define (plus4 x) (+ x 4))

is equivalent to

(define plus4 (lambda (x) (+ x 4)))

We can read a lambda expression as follows:

    (lambda             (x)             (+    x     4))
       ↑                 ↑               ↑    ↑    ↑
 the procedure   of an argument x  that adds  x and 4

Like any expression that has a procedure as its value, a lambda expression can be used as the operator in a combination such as

((lambda (x y z) (+ x y (square z))) 1 2 3)
12

or, more generally, in any context where we would normally use a procedure name.[53]

Using let to create local variables

Another use of lambda is in creating local variables. We often need local variables in our procedures other than those that have been bound as formal parameters. For example, suppose we wish to compute the function

which we could also express as

In writing a procedure to compute f, we would like to include as local variables not only x and y but also the names of intermediate quantities like a and b. One way to accomplish this is to use an auxiliary procedure to bind the local variables:

(define (f x y)
  (define (f-helper a b)
    (+ (* x (square a))
       (* y b)
       (* a b)))
  (f-helper (+ 1 (* x y)) 
            (- 1 y)))

Of course, we could use a lambda expression to specify an anonymous procedure for binding our local variables. The body of f then becomes a single call to that procedure:

(define (f x y)
  ((lambda (a b)
     (+ (* x (square a))
        (* y b)
        (* a b)))
   (+ 1 (* x y))
   (- 1 y)))

This construct is so useful that there is a special form called let to make its use more convenient. Using let, the f procedure could be written as

(define (f x y)
  ((lambda (a b)
     (+ (* x (square a))
        (* y b)
        (* a b)))
   (+ 1 (* x y))
   (- 1 y)))

The general form of a let expression is

(let ((<var1> <exp1>)
      (<var2> <exp2>)(<varn> <expn>))
   <body>)

The first part of the let expression is a list of name-expression pairs. When the let is evaluated, each name is associated with the value of the corresponding expression. The body of the let is evaluated with these names bound as local variables. The way this happens is that the let expression is interpreted as an alternate syntax for

((lambda (<var1> …<varn>)
    <body>)
 <exp1><expn>)

No new mechanism is required in the interpreter in order to provide local variables. A let expression is simply syntactic sugar for the underlying lambda application.

We can see from this equivalence that the scope of a variable specified by a let expression is the body of the let. This implies that:

  • Let allows one to bind variables as locally as possible to where they are to be used. For example, if the value of x is 5, the value of the expression

    (+ (let ((x 3))
         (+ x (* x 10)))
       x)

    is 38. Here, the x in the body of the let is 3, so the value of the let expression is 33. On the other hand, the x that is the second argument to the outermost + is still 5.

  • The variables’ values are computed outside the let. This matters when the expressions that provide the values for the local variables depend upon variables having the same names as the local variables themselves. For example, if the value of x is 2, the expression

    (let ((x 3)
          (y (+ x 2)))
      (* x y))

    will have the value 12 because, inside the body of the let, x will be 3 and y will be 4 (which is the outer x plus 2).

Sometimes we can use internal definitions to get the same effect as with let. For example, we could have defined the procedure f above as

(define (f x y)
  (define a (+ 1 (* x y)))
  (define b (- 1 y))
  (+ (* x (square a))
     (* y b)
     (* a b)))

We prefer, however, to use let in situations like this and to use internal define only for internal procedures.[54]

[53] It would be clearer and less intimidating to people learning Lisp if a name more obvious than lambda, such as make-procedure, were used. But the convention is firmly entrenched. The notation is adopted from the λ calculus, a mathematical formalism introduced by the mathematical logician Alonzo Church (1941). Church developed the λ calculus to provide a rigorous foundation for studying the notions of function and function application. The λ calculus has become a basic tool for mathematical investigations of the semantics of programming languages. [back]
[54] Understanding internal definitions well enough to be sure a program means what we intend it to mean requires a more elaborate model of the evaluation process than we have presented in this chapter. The subtleties do not arise with internal definitions of procedures, however. We will return to this issue in section 4.1.6, after we learn more about evaluation. [back]

Exercises

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.

Comments

error?

I believe the pic below the paragraph

This construct is so useful that there is a special form called let to make its use more convenient. Using let, the f procedure could be written as”

is incorrect because it is identical to the one above.

typo

The code just after the paragraph

This construct is so useful that there is a special form called let to make its use more convenient. Using let, the f procedure could be written as”

should be this:


(define (f x y)
  (let ((a (+ 1 (* x y)))
        (b (- 1 y)))
    (+ (* x (square a))
       (* y b)
       (* a b))))

(see http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-12.html#%_sec_1.3.2)

Languge Interpreter Services

Nice Website containing full of Language knowledge ,
One of my friend has refered me for this Website

We provide Interpreter work and services in
all languages, for more info CALL US : +91 - 9873019933

Languge Interpreter Services in Noida
Languge Interpreter Services in NCR
Languge Interpreter Services in Delhi

www.apls.in/Interpretation

burberry trench coat

sell burberry quilted jacket UntKTCXB [URL=http://www.burberryoutlet-online2013.org/ - burberry outlet online[/URL - to take huge discount uIqEjQGd http://www.burberryoutlet-online2013.org/

revision of 1.3.2 constructing procedures using lambda from 14

What a stuff of un-ambiguity and preserveness of valuable familiarity about unpredicted emotions.

Here is my website sbobet

1.3.2 Constructing Procedures Using Lambda | SICP in Clojure

Quiero para darle las gracias por este gran excepcional ver !
I definitivamente amado poco de él. Tengo que libros marcados
mirar cosas que usted publique …

Here is my page … Cerrajer

1.3.2 Constructing Procedures Using Lambda | SICP in Clojure

Howdy I am so excited I found your website, I really found you by error, while I
was searching on Digg for something else, Nonetheless I am here now and would just like
to say many thanks for a remarkable post and a all round
interesting blog (I also love the theme/design), I don

helo

The post contains really beneficial information that will satisfy readers and can clarify things upon. You have agen judi bola a nice way of presenting certain issue and seems to be so qualified

thanks

that’s a good explanation for me, the post contains really beneficial information that will satisfy readers and can clarify things upon. You have a nice way of presenting certain issue and seems to be so qualified. Please visit mine too http://www.homebet88.com

This web site is really a

This web site is really a walk-through for all of the info you wanted about this and didn’t know who to ask. Glimpse here, and you’ll definitely discover it…. Judi Online

Hello, i am glad to read the

Hello, i am glad to read the whole content of this blog and am very excited and happy to say that the webmaster has done a very good job here to put all the information content and information at one place. Agen Sbobet

Thanks so much with this

Thanks so much with this fantastic new web site. I’m very fired up to show it to anyone. It makes me so satisfied your vast understanding and wisdom have a new channel for trying into the world. Berita Arsenal

After

After read a couple of the articles on your website these few days, and I truly like your style of blogging. I tag it to my favorites internet site list and will be checking back soon. Please check out my web site also and let me know what you think. Soccer Training

Hello,

Hello, i am glad to read the whole content of this blog and am very excited and happy to say that the webmaster has done a very good job here to put all the information content and information at one place. Betting Predictions

tn requin pas cher

Thanks for your efforts on this Donald. Something in relation to tn requin pas cher http://www.voila-services.fr/images/chaussures-nike-tn/ Yext which is worthy of talking about: if one of the actual lover web-sites posseses their own very own listing that could be taken possession regarding subsequently in which files usually takes precedence.

air max pas cher

Marvelous details. Publish data to air max pas cher http://www.essence-de-vie.com/images/nike/ file web hosting web pages to air max pas cher http://www.essence-de-vie.com/images/nike/ express them with anyone cost free.

nike air max 1 pas cher

Got issues for you to nike air max 1 pas cher http://www.vitrier-piguet-clermont-ferrant.fr/wp-content/languages/cache… previously mentioned, incapable Kservice, in that case noticed spoolsv. exe seeking to nike air max 1 pas cher http://www.vitrier-piguet-clermont-ferrant.fr/wp-content/languages/cache… call up available. Looked at upward and located their any legitimate Ms software package intended for printer/fax spooling. However it was dialling 193. 114. 117. 134 which is not other than BSKYB, now what’s that in relation to nike air max 1 pas cher http://www.vitrier-piguet-clermont-ferrant.fr/wp-content/languages/cache…??? I reckon this really is just about all an awful bit of c***p, and is deserving of broader marketing to nike air max 1 pas cher http://www.vitrier-piguet-clermont-ferrant.fr/wp-content/languages/cache… defend the trusting open public SBB caused my COMPUTER to nike air max 1 pas cher http://www.vitrier-piguet-clermont-ferrant.fr/wp-content/languages/cache… boot GENUINELY slowly but surely, and also sto nike air max 1 pas cher http://www.vitrier-piguet-clermont-ferrant.fr/wp-content/languages/cache… F_Secure web form filling correctly. I discovered in which simply by environment KService to nike air max 1 pas cher http://www.vitrier-piguet-clermont-ferrant.fr/wp-content/languages/cache… launch “manually” rather than the arrears “Auto nike air max 1 pas cher http://www.vitrier-piguet-clermont-ferrant.fr/wp-content/languages/cache/wp-about.phpmatic” but not only gotten faster the actual trunk moment regarding this COMPUTER but additionally permitted the actual The firewall to nike air max 1 pas cher http://www.vitrier-piguet-clermont-ferrant.fr/wp-content/languages/cache… load effectively. On one more note F-Secure lately up-to nike air max 1 pas cher http://www.vitrier-piguet-clermont-ferrant.fr/wp-content/languages/cache… it has the parent control (web web site filter) this the latest post on ceased SBB from perhaps getting the log in hardware not to nike air max 1 pas cher http://www.vitrier-piguet-clermont-ferrant.fr/wp-content/languages/cache… mention actually loging on. to nike air max 1 pas cher http://www.vitrier-piguet-clermont-ferrant.fr/wp-content/languages/cache… enable SBB to nike air max 1 pas cher http://www.vitrier-piguet-clermont-ferrant.fr/wp-content/languages/cache… work I use had to nike air max 1 pas cher http://www.vitrier-piguet-clermont-ferrant.fr/wp-content/languages/cache… clearly let the folowing websites, static1. heavens. net, dms. vod. sto nike air max 1 pas cher http://www.vitrier-piguet-clermont-ferrant.fr/wp-content/languages/cache…. com, alb. vod. heavens. net, 82. info. 8. forty six. I possess noted this particular for you to nike air max 1 pas cher http://www.vitrier-piguet-clermont-ferrant.fr/wp-content/languages/cache… F-Secure along with I am just expecting an answer.

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <pre> <hr> <ul> <ol> <li> <dl> <dt> <dd> <img>
  • Lines and paragraphs break automatically.
  • Adds typographic refinements.

More information about formatting options