;; This is the code for -- Stable Marriage (define (match-make proposers proposees) (send proposers 'reset) (send proposees 'reset) (courtship proposers proposers proposees) (zip-together (send proposers 'name) (send (send proposers 'intended) 'name))) (define (courtship unengaged-proposers proposers proposees) ... ) (define (currently-unengaged list-of-people) ... ) (define (send list-of-people message) ... ) (define (couple? person1 person2) ...) (define (zip-together list1 list2) (if (null? list1) '() (cons (list (car list1) (car list2)) (zip-together (cdr list1) (cdr list2))))) (define (filter pred lst) (cond ((null? lst) '()) ((pred (car lst)) (cons (car lst) (filter pred (cdr lst)))) (else (filter pred (cdr lst))))) (define (make-person my-name) (let ((preference-list '()) (possible-mates '()) (current-intended '())) (define (me message) (cond ((eq? message 'name) my-name) ((eq? message 'intended) current-intended) ((eq? message 'loves) preference-list) ((eq? message 'possible) possible-mates) ((eq? message 'reset) (set! current-intended '()) (set! possible-mates preference-list) 'reset-done) ((eq? message 'load-preferences) (lambda (plist) (set! preference-list plist) (set! possible-mates plist) (set! current-intended '()) 'preferences-loaded)) ((eq? message 'propose) (let ((beloved (car possible-mates))) (set! possible-mates (cdr possible-mates)) (if (eq? ((beloved 'i-love-you) me) 'i-love-you-too) (begin (set! current-intended beloved) 'we-are-engaged) 'no-one-loves-me))) ((eq? message 'i-love-you) ... ) ((eq? message 'i-changed-my-mind) (lambda (lost-love) (cond ((eq? current-intended lost-love) (set! current-intended '()) 'dumped!) (else 'there-must-be-some-misunderstanding)))) (else (error "Bad message to a person " (list me my-name message))))) me)) ;; This is a test file for -- Stable Marriage (define alan (make-person 'Alan)) (define bob (make-person 'Bob)) (define charles (make-person 'Chuck)) (define david (make-person 'Dave)) (define ernest (make-person 'Ernie)) (define franklin (make-person 'Frank)) (define agnes (make-person 'Agnes)) (define bertha (make-person 'Bertha)) (define carol (make-person 'Carol)) (define deborah (make-person 'Debbie)) (define ellen (make-person 'Ellen)) (define francine (make-person 'Fran)) ((alan 'load-preferences) (list agnes carol francine bertha deborah ellen)) ((bob 'load-preferences) (list carol francine bertha deborah agnes ellen)) ((charles 'load-preferences) (list agnes francine carol deborah bertha ellen)) ((david 'load-preferences) (list francine ellen deborah agnes carol bertha)) ((ernest 'load-preferences) (list ellen carol francine agnes deborah bertha)) ((franklin 'load-preferences) (list ellen carol francine bertha agnes deborah)) ((agnes 'load-preferences) (list charles alan bob david ernest franklin)) ((bertha 'load-preferences) (list charles alan bob david ernest franklin)) ((carol 'load-preferences) (list franklin charles bob alan ernest david)) ((deborah 'load-preferences) (list bob alan charles franklin david ernest)) ((ellen 'load-preferences) (list franklin charles bob alan ernest david)) ((francine 'load-preferences) (list alan bob charles david franklin ernest)) (define men (list alan bob charles david ernest franklin)) (define women (list agnes bertha carol deborah ellen francine))