Вопрос

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.

Это было полезно?

Решение

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)

Другие советы

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>)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top