newLISP Fan Club

Forum => Anything else we might add? => Topic started by: gcanyon on September 23, 2006, 04:13:33 PM

Title: Puzzled by (let examples in the documentation
Post by: gcanyon on September 23, 2006, 04:13:33 PM
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? ;-)
Title:
Post by: Lutz on September 23, 2006, 04:22:30 PM
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
Title:
Post by: gcanyon on September 24, 2006, 12:46:02 PM
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
Title:
Post by: arunbear on October 08, 2006, 03:19:13 PM
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).