练习4-5

原文

Exercise 4.5. Scheme allows an additional syntax for cond clauses, ( => ). If evaluates to a true value, then is evaluated. Its value must be a procedure of one argument; this procedure is then invoked on the value of the , and the result is returned as the value of the cond expression. For example

(cond ((assoc 'b '((a 1) (b 2))) => cadr)
(else false))

returns 2. Modify the handling of cond so that it supports this extended syntax.

分析

代码

 (define (extended-cond-syntax? clause) (eq? (cadr clause) '=>))
(define (extended-cond-test clause) (car clause))
(define (extended-cond-recipient clause) (caddr clause))
(define (cond->if expr)
(expand-clauses (cond-clauses expr))) (define (expand-clauses clauses)
(if (null? clauses)
'false
(let ((first (car clauses))
(rest (cdr clauses)))
(cond ((cond-else-clause? first)
(if (null? rest)
(sequence->exp (cond-actions first))
(error "ELSE clause isn't last -- COND->IF" clauses)))
((extended-cond-syntax? first)
(make-if (extended-cond-test first)
(list (extended-cond-recipient first)
(extended-cond-test first))
(expand-clauses rest)))
(else
(make-if (cond-predicate first)
(sequence->exp (cond-actions first))
(expand-clauses rest)))))))


感谢您的訪问,希望对您有所帮助。

欢迎大家关注或收藏、评论或点赞。


为使本文得到斧正和提问。转载请注明出处:

http://blog.csdn.net/nomasp


【SICP练习】149 练习4.5的更多相关文章

  1. Lisp和SICP

         大概不少programmer都看过<黑客与画家>,作者用了整整一章的篇幅讨论Lisp的强大.我自然就会手痒痒.      几个月前,几天内攻城略地搞定了Python,用的方法便是 ...

  2. SICP— 第一章 构造过程抽象

    SICP  Structure And Interpretation Of Computer Programs 中文第2版 分两部分  S 和 I 第一章 构造过程抽象 1,程序设计的基本元素 2,过 ...

  3. 《SICP》读后感:关于软件本质的一点思考

    摘要:软件本身不是目的,人类的需求才是目的,而软件只是达到目的的手段. 软件的本质在于控制复杂性,这个复杂性并非来自于计算机,也并非来自于现实世界,而是来自于人类的思维和知识体系. 软件被使用的广泛性 ...

  4. SICP 习题 (1.7) 解题总结

    SICP 习题 1.7 是对正文1.1.7节中的牛顿法求平方根的改进,改进部分是good-enough?过程. 原来的good-enough?是判断x和guess平方的差值是否小于0.001,这个过程 ...

  5. SICP 习题 (1.14)解题总结

    SICP 习题 1.14要求计算出过程count-change的增长阶.count-change是书中1.2.2节讲解的用于计算零钱找换方案的过程. 要解答习题1.14,首先你需要理解count-ch ...

  6. SICP 习题 (1.8) 解题总结

    SICP 习题1.8需要我们做的是按照牛顿法求平方根的方法做一个求立方根的过程. 所以说书中讲牛顿法求平方根的内容还是要好好理解,不然后面这几道题做起来就比较困难. 反过来,如果理解了牛顿法求平方根的 ...

  7. sicp第1章

    牛顿迭代法求平方: (define (sqrt-iter guess x) (if (good-enough? guess x) guess (sqrt-iter (improve guess x) ...

  8. SICP 阅读笔记(二)

    Chapter 1: Building Abstractions with Procedures 2015-09-29 016 Preface of this chapter QUOTE: The a ...

  9. SICP 习题 (1.9) 解题总结

    SICP 习题 1.9 开始针对“迭代计算过程”和“递归计算过程”,有关迭代计算过程和递归计算过程的内容在书中的1.2.1节有详细讨论,要完成习题1.9,必须完全吃透1.2.1节的内容,不然的话,即使 ...

随机推荐

  1. 第4节 hive调优:2、数据倾斜

    数据的倾斜: 主要就是合理的控制我们的map个数以及reduce个数 第一个问题:maptask的个数怎么定的???与我们文件的block块相关,默认一个block块就是对应一个maptask 第二个 ...

  2. Highlights in a Journal

    ** Highlights **** example- b huang, 2016, Design and performance enhancement of a bi-directional co ...

  3. gitlab+jenkins+docker自动构建

    docker容器部署gitlab: sudo docker run --detach \ --hostname git.gitlab.com \ --net=host \ --publish 9443 ...

  4. Sticky footer经典布局--绝对底部布局

    原文转载于:https://cnodejs.org/topic/56ebdf2db705742136388f71 何为Sticky footer布局? 我们常见的网页布局方式一般分为header(页头 ...

  5. python3 时间复杂度

    时间复杂度 (1)时间频度 一个算法执行所耗费的时间,从理论上是不能算出来的,必须上机运行测试才能知道.但我们不可能也没有必要对每个算法都上机测试,只需知道哪个算法花费的时间多,哪个算法花费的时间少就 ...

  6. Bone Collector II(01背包kth)

    The title of this problem is familiar,isn't it?yeah,if you had took part in the "Rookie Cup&quo ...

  7. poj 1780 , poj 1392 欧拉回路求前后相互衔接的数字串

    两道题目意思差不多 第一题是10进制 , 第二题是2进制的 都是利用欧拉回路的fleury算法来解决 因为我总是希望小的排在前面,所以我总是先将较小数加入栈,再利用另一个数组接收答案,但是这里再从栈中 ...

  8. UVA10673 上下界问题

    #include <iostream> #include<cstdio> using namespace std; #define LL long long LL a,b,m, ...

  9. iview自定义配置

    说明 iview是一套基于 Vue.js 的高质量 UI 组件库.主要用户PC端页面设计. 官网:https://www.iviewui.com/ 1.在vue-cli项目中,添加该框架 第一步,安装 ...

  10. android去除标题栏和状态栏(全屏)

    转--http://www.eoeandroid.com/thread-66555-1-1.html 在开发中我们经常需要把我们的应用设置为全屏,这里我所知道的有俩中方法,一中是在代码中设置,另一种方 ...