【SICP練習】150 練習4.6,sicp練習1504.6

來源:互聯網
上載者:User

【SICP練習】150 練習4.6,sicp練習1504.6
練習4-6原文

Exercise 4.6. Let expressions are derived expressions, because
(let (( ) … ( )) )
is equivalent to
((lambda ( … ) ) )
Implement a syntactic transformation let->combination that reduces evaluating let expressions to evaluating combinations of the type shown above, and add the appropriate clause to eval to handle let expressions.

分析

這道題需要我們將let運算式轉換成相應的運算式。

(let (( ) … ( )) )

從上面的樣本我們可以看到在let表達中,假設其為expr,用cdr可以得到(( ) … ( )),然後再用高階函數map搭配car來取出所有的var。

同樣的,如果要取出exp部分,先用cadr,再用高階函數map搭配cadr。

而body部分用caddr就可以直接求出了。

然後就可以開始寫let->combination,其傳入一個參數expr。

((lambda ( … ) ) )

根據這段範例程式碼呢,我們先調用書中第256頁的make-lambda來構造前面的一部,這個函數有兩個參數parameter和body。

這樣一來就全部都完成了,當然了,還需要將let?加入到eval中,在此之前也要定義let?。同樣要調用第256頁的tagged-list?。那麼接下來就是具體的代碼咯。

代碼
(define (let-vars expr) (map car (cadr expr)))(define (let-exp expr) (map cadr (cadr expr)))(define (let-body expr) (caddr expr))(define (let->combination expr)  (cons (make-lambda (let-vars expr) (let-body expr))    (let-exp expr)))(define (let? expr) (tagged-list? expr 'let))((let? expr) (eval (let->combination expr) env))

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.