examples/test-tally-and-accumulate.ss
;; Test Tally and Accumulate
(require (planet "simulation-with-graphics.ss"
                 ("williams" "simulation.plt")))

(define tallied #f)
(define accumulated #f)

(define-process (test-process value-duration-list)
  (let loop ((vdl value-duration-list))
    (when (not (null? vdl))
      (let ((value (caar vdl))
            (duration (cadar vdl)))
        (set-variable-value! tallied value)
        (set-variable-value! accumulated value)
        (wait duration)
        (loop (cdr vdl))))))

(define (main value-duration-list)
  (with-new-simulation-environment
    (set! tallied (make-variable))
    (tally (variable-statistics tallied))
    (tally (variable-history tallied))
    (set! accumulated (make-variable))
    (accumulate (variable-statistics accumulated))
    (accumulate (variable-history accumulated))
    (schedule (at 0.0) (test-process value-duration-list))
    (start-simulation)
    (printf "--- Test Tally and Accumulate ---~n")
    (printf "~n--- Tally ---~n")
    (printf "N    = ~a~n" (variable-n tallied))
    (printf "Sum  = ~a~n" (variable-sum tallied))
    (printf "Mean = ~a~n" (variable-mean tallied))
    (printf "~a~n" (history-plot (variable-history tallied)))
    (printf "~n--- Accumulate ---~n")
    (printf "N    = ~a~n" (variable-n accumulated))
    (printf "Sum  = ~a~n" (variable-sum accumulated))
    (printf "Mean = ~a~n" (variable-mean accumulated))
    (printf "~a~n" (history-plot (variable-history accumulated)))))

(main '((1 2)(2 1)(3 2)(4 3)))