MIT Scheme Message Passing Abstraction -
in computer science course taking, homework, tasked several different questions pertaining message passing. have been able solve one, asks following:
write mailman object factory
(make-mailman)
takes in no parameters , returns message-passing object responds following messages:
'add-to-route
: return procedure takes in arbitrary number of mailbox objects , adds them mailman object's “route”'collect-letters
: return procedure takes in arbitrary number of letter objects , collects them future distribution'distribute
: add each of collected letters mailbox on mailman's route address matches letter's destination , return list of letters destinations did not match mailboxes on route (note: after each passing of'distribute
mailman object should have no collected letters.)
some remarks given make code easier include:
if multiple letters distributed same mailbox in 1 distribution round, 1 of them may “latest” letter message returned passing
'get-latest-message
mailbox.no 2 mailboxes have same address.
no mailbox or letter passed mailman more once.
the bad letters returned distribute not need in specific order.
use
. args
syntax accepting arbitrary amount of arguments.
this have been able figure out myself:
(define (make-mailman) (let ((t '())) (define (route-adder . mobjects) (assoc mobjects t)) (define (letter-collecter . lobjects) (assoc lobjects t)) (define (add-to-route mobjects) (begin (set! t (cons (route-adder . mobjects) t)) 'done)) (define (collect-letters lobjects) (begin (set! t (cons (sort-strings (letter-collecter . lobjects)) t)) 'done)) (define (dispatch z) (cond ((eq? z 'add-to-route) add-to-route) ((eq? z 'collect-letters) collect-letters) ((eq? z 'distribute) "unsure of here") (else "invalid option"))) dispatch))
any can given me here appreciated, have tried looking @ problem while, , cannot figure out here.
your code has kinds of mix-ups. :) let's proceed step step.
the dispatch
bit ok:
(define (make-mailman) (let ... ... (define (dispatch msg) ;; use short suggestive var names (cond ((eq? msg 'add-to-route) add-to-route) ((eq? msg 'collect-letters) collect-letters) ((eq? msg 'distribute) ;; "unsure of here" <<-- distribute letters, else? distribute-the-letters) (else "invalid option"))) dispatch))
with such objects, sample call (define ob (make-mailman))
, ((ob 'add-to-route) box1 box2 ... boxn)
etc. add-to-route
procedure must defined way:
(define (make-mailman) (let ((self (list '(route) ; each mailman has route, , mailbag '(mailbag)))) ; use suggestive name here (t, t?) ... (define (add-to-route . mailboxes) (let ((route (assoc 'route self))) (set-cdr! route (append mailboxes ; there no duplicates (cdr route))) 'done))
right? same letters:
(define (collect-letters . letters) (let ((mailbag (assoc 'mailbag self))) ..... 'done))
now can deal missing part, distribute-the-letters
:
(define (distribute-the-letters) ;; each letter in mailbag (let* ((mailbag (assoc 'mailbag self)) (mailboxes (cdr (assoc 'route self))) (letters (cdr mailbag))) (if (null? letters) () (let loop ((letter (car letters)) (letters (cdr letters)) (not-delivered ())) ;; access address, (let* ((address (letter 'get-address)) ;; (we assume supports interface, ;; or maybe that's part of previous assignment) ;; , find mailbox on route such (mbx (find-mailbox address mailboxes))) ;; address matches letter's ;; , if so, (if ..... ;; put letter mailbox: ((mbx 'put-letter) letter) ;; (we assume supports interface, ;; or maybe that's part of previous assignment) ;; if not, add letter "not-delivered" list ..... ) (if (null? letters) ;; having emptied mailbag, return "not-delivered" list (begin (set-cdr! mailbag nil) not-delivered) (loop (car letters) (cdr letters) not-delivered)))))))
we assume both letter
, mailbox
objects support message type 'get-address
both return same comparable address
type of object, , mailbox
objects support 'put-letter
message.
Comments
Post a Comment