How to express let* as a lambda expression (not the regular let)

末鹿安然 提交于 2019-11-28 08:06:59

问题


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.


回答1:


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)



回答2:


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


来源:https://stackoverflow.com/questions/16698548/how-to-express-let-as-a-lambda-expression-not-the-regular-let

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!