累计器的代码片段写法[#29]

code-fragment返回片段值,不执行其中的define。operations保存累计操作,state保存total的初始值。作者可以围绕操作解释程序,而在装配中明确状态的位置:

@(define operations
   (code-fragment "lp-accumulate" "累计器的 accumulate 定义"
     #:requires (total)
     (define (accumulate delta)
       (set! total (+ total delta))
       total)))
@(define state
   (code-fragment "lp-accumulator-state" "累计器的 total 初始状态"
     (define total 0)))

fragment-tree才产生阅读内容。它使用片段的原文与稳定ID;requires明确声明操作依赖total,不能代替完整的依赖分析。

program. 累计器的 accumulate 定义[lp-accumulate]

Defines: (accumulate)

Assembled in 累计器的装配计划

Outputs: accumulator.scm

Requires: total

(define (accumulate delta)
       (set! total (+ total delta))
       total)

program. 累计器的 total 初始状态[lp-accumulator-state]

Defines: (total)

Assembled in 累计器的装配计划

Outputs: accumulator.scm

(define total 0)