Puzzled by (let examples in the documentation

Started by gcanyon, September 23, 2006, 04:13:33 PM

Previous topic - Next topic

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?


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


(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? ;-)

Lutz

#1
QuoteWouldn't this make more sense in defining a sum-of-squares routine?


Yes, absolutely, the example has been kept simple for didactic purposes.



Lutz

gcanyon

#2
Quote from: "Lutz"
QuoteWouldn'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

arunbear

#3
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:


(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).