【SICP練習】151 練習4.7,sicp練習1514.7

來源:互聯網
上載者:User

【SICP練習】151 練習4.7,sicp練習1514.7
練習4-7原文

Exercise 4.7. Let* is similar to let, except that the bindings of the let variables are performed sequentially from left to right, and each binding is made in an environment in which all of the preceding bindings are visible. For example

(let* ((x 3)             (y (+ x 2))         (z (+ x y 5)))     (* x z))

returns 39. Explain how a let* expression can be rewritten as a set of nested let expressions, and write a procedure let*->nested-lets that performs this transformation. If we have already implemented let (exercise 4.6) and we want to extend the evaluator to handle let*, is it sufficient to add a clause to eval whose action is

(eval (let*->nested-lets exp) env)

or must we explicitly expand let* in terms of non-derived expressions?

分析

這道題和上一道很類似,抓住題中的要點就會迎刃而解啦。那就是說從左至右求值,那麼我們可以用list和car以及cdr來完成,核心思想是用遞迴,不斷的向右邊推進,直到exp為空白,此時就返回body,然後結束構造。至於tagged-list?這些和上一題都是一樣的。

代碼
(define (let*? expr) (tagged-list? expr 'let*))(define (let*-body expr) (caddr expr))(define (let*-exp expr) (cadr expr))(define (let*->nested-lets expr)  (let ((exp (let*-exp expr))    (body (let*-body expr)))    (defien (make-lets exprs)      (if (null? exprs)      body      (list 'let        (list (car exprs))        (make-lets (cdr exprs)))))    (make-lets exp)))

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.