【SICP練習】145 練習4.1,sicp練習1454.1

來源:互聯網
上載者:User

【SICP練習】145 練習4.1,sicp練習1454.1
練習4-1原文

Exercise 4.1. Notice that we cannot tell whether the metacircular evaluator evaluates operands from left to right or from right to left. Its evaluation order is inherited from the underlying Lisp: If the arguments to cons in list-of-values are evaluated from left to right, then list-of-values will evaluate operands from left to right; and if the arguments to cons are evaluated from right to left, then list-ofvalues will evaluate operands from right to left.Write a version of list-of-values that evaluates operands from left to right regardless of the order of evaluation in the underlying Lisp. Also write a version of list-of-values that evaluates operands from right to left.

分析

如題中所說的,list-of-values的求值順序由cons的參數而定。同時也要使用no-operands?來判斷exps,如果為真則返回空。而左右之分,在let中的求值順序可以決定。如果是從左至右就應該是先求值first而後求值rest,最後用cons來構造到一起即可。

代碼
 (define (list-of-values-l-to-r exps env)    (if (no-operands? exps)        '()        (let ((first (eval (first-operand exps) env)))          (let ((rest (list-of-values-l-to-r (rest-operands exps) env)))            (cons first rest)))))  (define (list-of-values-r-to-l exps env)    (if (no-operands? exps)        '()        (let ((rest (list-of-values-r-to-l (rest-operands exps) env)))          (let ((first (eval (first-operand exps) env)))            (cons first rest))))) 

聯繫我們

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