关于SICP3.3.4的agenda
书中只说了after-delay和propagate的实现,但是没有提到其中agenda的实现.
我看了一下置顶,似乎答案里也没有提到这个...
但是为了能让里面的东西跑起来,我自己实现了一个agenda,经过了简单的测试.
如果以后有人做到这里,可以拿来参考一下
技巧拙劣,还望指教:
(其中propagate的用法略有不同,我增加了一个表参数
还擅自增加了一个"执行到设定的某个时间停止"的操作act-to-time,第一个参数是时间(没有判断是否会逆向),第二个参数是待处理表)
- ;Agenda
- (define (get-time pro-element)
- (car pro-element))
- (define (get-action pro-element)
- (cdr pro-element))
- (define (make-agenda)
- (cons 0 '()))
- (define (empty-agenda? agenda)
- (null? (cdr agenda)))
- (define (first-agenda-item agenda)
- (cadr agenda))
- (define (remove-first-agenda-item! agenda)
- (if (empty-agenda? agenda) (display "Try to delete an empty agenda")
- (set-cdr! agenda (cddr agenda)))
- 'done)
- (define (set-agenda-time! time agenda)
- (set-car! agenda time))
- (define (add-to-agenda! time action agenda)
- (if (or (null? (cdr agenda))
- (> (get-time (cadr agenda)) time)) (let ((new-procedure (cons time action)))
- (let ((new-list (cons new-procedure (cdr agenda))))
- (set-cdr! agenda new-list)
- 'ok))
- (add-to-agenda! time action (cdr agenda))))
- (define (action-first! agenda)
- (if (empty-agenda? agenda) (display "Try to act an empty agenda!")
- (let ((first-element (first-agenda-item agenda)))
- ((get-action first-element))
- (set-agenda-time! (get-time first-element) agenda)
- (remove-first-agenda-item! agenda)
- 'ok
- )))
- (define (act-to-time! time agenda)
- (if (or (empty-agenda? agenda)
- (< time (get-time (first-agenda-item agenda)))) (begin
- (set-agenda-time! time agenda)
- 'done)
- (begin
- (set-agenda-time! (get-time (first-agenda-item agenda)) agenda)
- (action-first! agenda)
- (act-to-time! time agenda))))
- (define (current-time agenda)
- (car agenda))
- (define (propagate agenda)
- (if (empty-agenda? agenda) 'done
- (begin
- (action-first! agenda)
- (propagate agenda))))
- (define (reset-agenda! agenda)
- (set-agenda-time! 0 agenda)
- (set-cdr! agenda '())
- 'done)
- (define the-agenda (make-agenda))
- (define (after-delay delay action)
- (add-to-agenda! (+ delay (current-time the-agenda))
- action
- the-agenda))
复制代码
[ 本帖最后由 PeterGhostWolf 于 2009-3-17 18:12 编辑 ]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我顿时泪奔了....
不过可以看一下自己的实现和书上的区别和差距...
书上有的。
第 195 页下方,有一节:“待处理表的实现”
不过自己实现一个比从书上抄更好,支持! :wink: