6

I am writting a script for GIMP and using let* as it was in a sample I took. But it seems to be just a lambda sugar exactly like let. Why are they different? What is the difference between them?

1

3 Answers 3

10

They are different in the order in which variables are bound. Consider this for example:

> (let ((a 1)(b (+ a 2))) b)

This code will FAIL because b requires a, which has not been defined before. It is defined, in the same let, but Scheme will take all your let definitions as only one statement and not allow them to reference each other. In Gambit Scheme, it raises:

*** ERROR IN ##raise-unbound-global-exception -- Unbound variable: a

Conversely, let* will bind the first variable of the let, then the second, etc... so:

> (let* ((a 1)(b (+ a 2))) b)
3

Works as expected.

A third form which is of interest is letrec which lets not only variables in the let reference other variables, but also let them reference themselves (e.g. for recursion). This lets you write code like:

> (letrec ((f (lambda(n) ;; Takes the binary log2 recursively
               (cond
                ((= n 1) 0)
                (else (+ 1 (f (/ n 2))))))))
   (f 256)) ;; 2^8 = 256
8

If you try to define a recursive function with let or let*, it will tell you the variable is unbound.

All of this can be achieved via clever rearranging/nesting of the let statements, but let* and letrec can be more convenient and readable in some cases like these.

4

They are different in the way they bind variables. All variables in one let uses the same lambda-form, so you can do this:

(let ((x 10) (y 20))
  (let ((x y) (y x))
     (display (list x y)))) ; prints (20 10)

While switch the inner let with a let* and you'll se that the second binds towards what was bound in the first binding and not what was before the let*

(let ((x 10) (y 20))
  (let* ((x y) (y x))
     (display (list x y)))) ; prints (20 20)

the reason for this is that

(let* ((x y) (y x))
   ...)

is the same as

(let ((x y))
  (let ((y x))
    ...))
2

let and let* are used for binding variables and both are syntactic sugar (macro), but let* binds variables one after the other soon (from left to right, or from up to down). The difference is also the different scope. In let the scope of each variable is only the expression, not the bindings. In let* the scope of each variable is the expression and the before bindings. With let* you do such a thing (b a)

...
(let* ((a 1)
       (b a))
   ...)
...

with let you don't.

Implementation of let:

(define-syntax let
 (syntax-rules ()
  ((_ (( variable value ) ...) body ...)
   (( lambda (variable ...) body ...) value ...))))

Implementation of let*:

(define-syntax let*
  (syntax-rules ()
    ; pattern for one binding
    ((_ ((variable value)) body ...)
      ((lambda (variable) body ...) value))
    ; pattern for two or more bindings
    ((_ ((variable value) . other) body ...)
      ((lambda (variable) (let* other body ...)) value))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.