免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3196 | 回复: 2
打印 上一主题 下一主题

关于SICP3.3.4的agenda [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-03-17 18:07 |只看该作者 |倒序浏览
书中只说了after-delay和propagate的实现,但是没有提到其中agenda的实现.
我看了一下置顶,似乎答案里也没有提到这个...
但是为了能让里面的东西跑起来,我自己实现了一个agenda,经过了简单的测试.
如果以后有人做到这里,可以拿来参考一下
技巧拙劣,还望指教:
(其中propagate的用法略有不同,我增加了一个表参数
还擅自增加了一个"执行到设定的某个时间停止"的操作act-to-time,第一个参数是时间(没有判断是否会逆向),第二个参数是待处理表)

  1. ;Agenda

  2. (define (get-time pro-element)
  3.   (car pro-element))

  4. (define (get-action pro-element)
  5.   (cdr pro-element))

  6. (define (make-agenda)
  7.   (cons 0 '()))

  8. (define (empty-agenda? agenda)
  9.   (null? (cdr agenda)))

  10. (define (first-agenda-item agenda)
  11.   (cadr agenda))

  12. (define (remove-first-agenda-item! agenda)
  13.   (if (empty-agenda? agenda) (display "Try to delete an empty agenda")
  14.       (set-cdr! agenda (cddr agenda)))
  15.   'done)

  16. (define (set-agenda-time! time agenda)
  17.   (set-car! agenda time))

  18. (define (add-to-agenda! time action agenda)
  19.   (if (or (null? (cdr agenda))
  20.           (> (get-time (cadr agenda)) time)) (let ((new-procedure (cons time action)))
  21.                                                (let ((new-list (cons new-procedure (cdr agenda))))
  22.                                                  (set-cdr! agenda new-list)
  23.                                                  'ok))
  24.       (add-to-agenda! time action (cdr agenda))))

  25. (define (action-first! agenda)
  26.   (if (empty-agenda? agenda) (display "Try to act an empty agenda!")
  27.       (let ((first-element (first-agenda-item agenda)))
  28.         ((get-action first-element))
  29.         (set-agenda-time! (get-time first-element) agenda)
  30.         (remove-first-agenda-item! agenda)
  31.         'ok
  32.         )))

  33. (define (act-to-time! time agenda)
  34.   (if (or (empty-agenda? agenda)
  35.           (< time (get-time (first-agenda-item agenda)))) (begin
  36.                                                            (set-agenda-time! time agenda)
  37.                                                            'done)
  38.       (begin
  39.         (set-agenda-time! (get-time (first-agenda-item agenda)) agenda)
  40.         (action-first! agenda)
  41.         (act-to-time! time agenda))))
  42.       

  43. (define (current-time agenda)
  44.   (car agenda))

  45. (define (propagate agenda)
  46.   (if (empty-agenda? agenda) 'done
  47.       (begin
  48.         (action-first! agenda)
  49.         (propagate agenda))))

  50. (define (reset-agenda! agenda)
  51.   (set-agenda-time! 0 agenda)
  52.   (set-cdr! agenda '())
  53.   'done)

  54. (define the-agenda (make-agenda))

  55. (define (after-delay delay action)
  56.   (add-to-agenda! (+ delay (current-time the-agenda))
  57.                   action
  58.                   the-agenda))
复制代码

[ 本帖最后由 PeterGhostWolf 于 2009-3-17 18:12 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2009-03-17 18:24 |只看该作者
书上有的。

第 195 页下方,有一节:“待处理表的实现”

不过自己实现一个比从书上抄更好,支持! :wink:

论坛徽章:
0
3 [报告]
发表于 2009-03-17 18:31 |只看该作者
原帖由 win_hate 于 2009-3-17 18:24 发表
书上有的。

第 195 页下方,有一节:“待处理表的实现”

不过自己实现一个比从书上抄更好,支持! :wink:

我顿时泪奔了....
不过可以看一下自己的实现和书上的区别和差距...
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP