zoukankan      html  css  js  c++  java
  • 【SICP练习】152 练习4.8

    练习4-8

    原文

    Exercise 4.8. “Named let” is a variant of let that has the form

    (let <var> <bindings> <body>)

    The and are just as in ordinary let, except that is bound within to a procedure whose body is and whose parameters are the variables in the . Thus, one can repeatedly execute the by invoking the procedure named . For example, the iterative Fibonacci procedure (section 1.2.2) can be rewritten using named let as follows:

    (define (fib n) 
       (let fib-iter ((a 1)    
                      (b 0)    
                      (count n))
          (if (= count 0)      
              b      
              (fib-iter (+ a b) a (- count 1)))))

    Modify let->combination of exercise 4.6 to also support named let.

    分析

    希望大家还是有事没事看看原文啦,我才发现见过很多次的modify原来是修改的意思。

    关于named let的一些比较什么的,大家可以看这里:【Scheme归纳】3 比较do, let, loop

    从题目的代码中我们也可以看到named-let的名字可以用cadr来取出,也就是书中的fib-iter。而body部分从以下代码中也可以看出来得用3个cdr和1个car。

    (let <var> <bindings> <body>)

    而parameter题中已经说了是binding中变量,取出binding用caddr,而取出题目示例中的a、b和count等则用map和car即可。取出题目示例中的1、0和n则用map和cadr。

    那么接下来我们还需要将named-let转换成函数,用list来构造这些就好,首先当然是’define,然后再用cons把name和parameter构造在一起,最后就是body啦。

    当然了,在let->combination中我们需要判断是不是named-let?,那么怎么判断呢,先判断是否是let?,再判断expr的名字是不是符号(symbol?)。

    最后就可以写let-combination啦。首先用写好的named-let?谓词来进行判断expr,然后为真的话就调用第257页的sequence->exp函数,否则就用cons来继续构造了。

    代码

    
    (define (named-let-name expr) 
      (cadr expr))
    
    (define (named-let-body expr)
      (cadddr expr))
    
    (define (named-let-parameters expr)
      (map car (caddr expr)))
    
    (define (named-let-exp expr)
      (map cadr (caddr expr)))
    
    (define (named-let? expr)
      (and (let? expr) (symbol? (cadr expr))))  
    
    (define (named-let->func expr)
      (list 'define
        (cons (named-let-name epxr)
              (named-let-parameters expr))
        (named-let-body expr)))
    
    (define (let->combination expr)
      (if (named-let? expr)
          (sequence->exp
           (list (named-let->func expr)
             (cons (named-let-name expr) 
               (named-let-exp expr))))
          (cons (make-lambda (let-vars expr)
                 (list (let-body expr)))
            (let-exp expr))))
    



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

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

  • 相关阅读:
    数据结构 图的接口定义与实现分析(超详细注解)
    数据结构-优先队列 接口定义与实现分析
    什么是梯度?
    javascript当中火狐的firebug如何单步调试程序?
    给出一个javascript的Helloworld例子
    lvs dr 模型配置详解
    数据库连接池
    JDBC事务管理
    JDBC工具类:JDBCUtils
    详解JDBC对象
  • 原文地址:https://www.cnblogs.com/NoMasp/p/4786042.html
Copyright © 2011-2022 走看看