examples/test-tally-and-accumulate.ss
#lang scheme/base
;; Test Tally and Accumulate

(require (planet williams/simulation/simulation-with-graphics))

(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))
    (write-special (history-plot (variable-history tallied)))
    (newline)
    (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))
    (write-special (history-plot (variable-history accumulated)))
    (newline)))

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