(Lispex)sicp.io
3.8 · 이산 사건 시뮬레이션

agenda는 시뮬레이션 시간을 명시적인 데이터로 만든다.

시간표가 붙은 동작을 정렬된 agenda에 넣으면 등록 순서와 달라도 가장 이른 시뮬레이션 시간부터 처리할 수 있습니다.

생각해 볼 질문

시뮬레이션은 등록된 순서를 따르지 않고 다음 사건을 어떻게 고를까요?

  • Represent an event as explicit time and action data
  • Insert events in nondecreasing timestamp order
  • Preserve scheduling order between events at the same time
  • Accumulate state by processing the earliest event first

schedule walks an already ordered agenda until the new time belongs before the current event. Equal timestamps do not satisfy the strict less-than test, so a new event at that time is inserted after events already there. The returned list exposes both simulated time and tie order.

propagate removes the earliest event, applies its numeric change to signal, and records the resulting state beside that event time. The agenda models logical simulation time rather than waiting for a wall clock, and these examples do not model simultaneous-event physics beyond their explicit insertion rule.

리스펙스 · SICP 코드SICP에 필요한 Scheme 호환 문법을 리스펙스 SICP 프로필로 실행합니다.
(begin
  (define (schedule time action agenda)
    (let ((event (list time action)))
      (cond ((null? agenda) (list event))
            ((< time (car (car agenda))) (cons event agenda))
            (else
             (cons (car agenda)
                   (schedule time action (cdr agenda)))))))
  (define agenda
    (schedule 5 'close
      (schedule 2 'sense
        (schedule 5 'open '()))))
  agenda)
리스펙스 학습용 런타임리스펙스 SICP 프로필 1.0.0
리스펙스 SICP 런타임 불러오는 중
리스펙스 · SICP 코드UTF-8 407 / 1,048,576바이트
예제
결과
출력
진단
보이는 실행 흐름0 / 0 개의 실행 이벤트
    이 브라우저 결과는 리스펙스 바우치나 권한이 아닙니다.wasm —
    예상 관찰

    The first program returns ((2 sense) (5 open) (5 close)). The second returns ((2 1) (4 4) (5 14)).

    실행 흐름에서 볼 점

    Follow schedule comparisons until each event reaches its timestamp position. Then watch propagate remove times 2, 4, and 5 in order while signal becomes 1, 4, and 14. These traces describe this agenda and its explicit tie rule only.

    직접 해보기

    힌트를 보기 전에 프로그램을 바꿔 보세요.

    Schedule an event named reset at time 1 in the first program. Predict its position without changing the insertion rule.

    힌트 하나 보기

    The new timestamp is earlier than every event already in the agenda.