zoukankan      html  css  js  c++  java
  • 【SICP练习】151 练习4.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)))
    



    为使本文得到斧正和提问,转载请注明出处:
    http://blog.csdn.net/nomasp

    版权声明:本文为 NoMasp柯于旺 原创文章,如需转载请联系本人。

  • 相关阅读:
    关于ugc的一点思考
    Fenng早年间对推荐系统的思考
    对于软件开发的一些思考
    并发排序
    Standford CoreNLP使用
    做事情的方式
    JAVA! static的作用
    struts2学习笔记--使用Validator校验数据
    Struts2中的ModelDriven机制及其运用
    ValueStack与ContentMap (ActionContext.getContext().getValueStack().set())
  • 原文地址:https://www.cnblogs.com/NoMasp/p/4786043.html
Copyright © 2011-2022 走看看