;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname today-server) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) (require 2htdp/universe) ; A Bundle is ; (make-bundle Universe [Listof Mail] [Listof IWorld]) ; A Mail is a (make-mail IWorld SExp) ;; Universe IWorld -> Bundle (define (join u iw) (make-bundle (make-pair (pair-count u) (cons iw (pair-worlds u))) empty empty)) ;; [Listof IWorld] -> [Listof Mail] (define (broadcast-reset iws) (map (λ (iw) (make-mail iw 'reset!)) iws)) ;; Universe IWorld Msg -> Bundle (define (process u iw msg) (cond [(> (pair-count u) 10) (make-bundle (make-pair 0 (pair-worlds u)) (broadcast-reset (pair-worlds u)) empty)] [else (make-bundle (make-pair (add1 (pair-count u)) (pair-worlds u)) empty empty)])) ;; A Universe is a (make-pair Natural [Listof IWorld]) (define-struct pair (count worlds)) (universe (make-pair 0 empty) (on-new join) (on-msg process))