Page 1 of 1

Puzzled by (let examples in the documentation

Posted: Sat Sep 23, 2006 11:13 pm
by gcanyon
The examples given for the use of (let are leaving me confused. I think I understand what (let is for, but in the context of the example, isn't it unnecessary?

Code: Select all

    (define (sum-sq a b)
      (let ((x (* a a)) (y (* b b)))
        (+ x y)))

    (sum-sq 3 4)  → 25

    (define (sum-sq a b)            ; alternative syntax
        (let (x (* a a) y (* b b))
            (+ x y)))
Wouldn't this make more sense in defining a sum-of-squares routine?

Code: Select all

(define (sum-sq a b)
          (+ (* a a) (* b b)))
I know it doesn't demonstrate the use of (let, but (let seems unnecesssary: I checked, and a and b don't seem to be affected outside this function. So what's the point of using (let to isolate values here? Or is it just an artificial example and I shouldn't worry so much? ;-)

Posted: Sat Sep 23, 2006 11:22 pm
by Lutz
Wouldn't this make more sense in defining a sum-of-squares routine?
Yes, absolutely, the example has been kept simple for didactic purposes.

Lutz

Posted: Sun Sep 24, 2006 7:46 pm
by gcanyon
Lutz wrote:
Wouldn't this make more sense in defining a sum-of-squares routine?
Yes, absolutely, the example has been kept simple for didactic purposes.

Lutz
Thanks, just making sure I wasn't missing something.

gc

Posted: Sun Oct 08, 2006 10:19 pm
by arunbear
The point is to localise x and y, not a and b.
e.g. suppose x and y were created with set rather than let:

Code: Select all

(define (sum-sq a b)
    (set 'x (* a a))
    (set 'y (* b b))
    (+ x y))

(println (sum-sq 3 4))
(println x)
(println y)
In this case x and y continue to exist outside of the scope they were created in (whereas they would not in the let version).