Question

I have a scheme related question, how can we implement let* as a lambda expression. To be more precise, I am not wondering about the "regular" let, but the let with * which lets us use one let expression within another.

Was it helpful?

Solution

The let* form is a series of nested lambdas. For example, this:

(let* ((a 10)
       (b (+ 10 a)))
  (+ a b))

Is equivalent to this:

((lambda (a)
   ((lambda (b)
      (+ a b))
    (+ 10 a)))
 10)

OTHER TIPS

Since you are not wondering about the 'regular' let, if a let* can be converted into let then you will have your answer. Therefore know that:

(let* ((a ...) (b ...) (c ...)) body ...)

is equivalent to:

(let ((a ...))
  (let ((b ...))
    (let ((c ...))
      body ...)))

(see R5RS, page 44, (define-syntax let* ...)). Now, given this, and knowledge that:

  (let ((a ...)) body ...)

is equivalent to:

  ((lambda (a) body ...) ...)

the 'expansion' of the let* that I showed above becomes:

  ((lambda (a)
     ((lambda (b)
        ((lambda (c)
           body ...)
          <c-init>))
      <b-init>))
   <a-init>)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top