Sono indaffarato su altre cose ma nei momenti di pausa torno a SICP, come adesso che proseguendo da qui copio qui.

Usare `let`

per creare variabili locali

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

`a = 1 + xy b = 1 - y f(x, y) = x * a^2 + y * b + a * b`

In writing a procedure to compute ** f**, we would like to include as local variables not only

**and**

`x`

**but also the names of intermediate quantities like**

`y`

**and**

`a`

**. One way to accomplish this is to use an auxiliary procedure to bind the local variables:**

`b`

`(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

**then becomes a single call to that procedure:**

`f`

`(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

**, the**

`let`

**procedure could be written as**

`f`

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

The general form of a ** let** expression is

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

which can be thought of as saying

`let <var1> have the value <exp1> and <var2> have the value <exp2> and <varn> have the value <expn> in <body>`

The first part of the ** let** expression is a list of name-expression pairs. When the

**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**

`let`

`((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

**application.**

`lambda`

We can see from this equivalence that the scope of a variable specified by a ** let** expression is the body of the

**. 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`let`

is 5, the value of the expression`x`

Here, the`(+ (let ((x 3))`

(+ x (* x 10)))

x)

in the body of the`x`

is 3, so the value of the let expression is 33. On the other hand, the`let`

that is the second argument to the outermost`x`

is still 5.`+`

- The variables’ values are computed outside the
. 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`let`

is 2, the expression`x`

will have the value 12 because, inside the body of the`(let ((x 3)`

(y (+ x 2)))

(* x y))

,`let`

will be 3 and`x`

will be 4 (which is the outer`y`

plus 2). Basta controllare le parentesi e tutto torna chiaro😉`x`

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

**above as**

`f`

`(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

**only for internal procedures.**

`define`

*: 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 [lontano futuro chissà…], after we learn more about evaluation.*

**Note*** Uh!* pausa che devo assimilare bene😳