Hi,<br> I have doubt in SICP exercise 3.47. I have found two solutions<br> but totally different. Can somebody please tell me which is<br> correct solutions and why?<br> I am pasting both the solutions below along with the link,<br>
1)<br><a href="http://git.wizardbook.org/wizardbook/tree/3.4.2/make-mutex-semaphore.scm">http://git.wizardbook.org/wizardbook/tree/3.4.2/make-mutex-semaphore.scm</a><br><br>;; May be cheating in the sense that make-non-blocking-mutex is a thin<br>
;; wrapper around test-and-set!.<br>(define (make-mutex-semaphore n)<br> (define (make-mutices n)<br> (let make-mutices ((i 0)<br> (mutices '()))<br> (if (< i n)<br> (make-mutices (+ i 1) (cons (make-non-blocking-mutex) mutices))<br>
mutices)))<br> (let ((mutices (make-mutices n)))<br> (define (acquire)<br> (let ((acquirendum<br> (find-matching-item mutices<br> (lambda (mutex) (false? (mutex 'acquire))))))<br>
(if (not acquirendum) (acquire))))<br> (define (release)<br> (let ((relaxandum<br> (find-matching-item mutices<br> (lambda (mutex) (mutex 'release)))))<br> (if relaxandum<br>
(relaxandum 'release)<br> (error "No acquired mutices -- RELEASE" mutices))))<br> (define (dispatch m)<br> (cond ((eq? m 'acquire) (acquire))<br> ((eq? m 'release) (release))<br>
(else (error "Unknown request" m))))<br> dispatch))<br><br>2)<br><a href="http://eli.thegreenplace.net/2007/10/26/sicp-334/">http://eli.thegreenplace.net/2007/10/26/sicp-334/</a><br><br>(define (make-semaphore-mtx maximal)<br>
(let ((count maximal)<br> (mutex (make-mutex)))<br> (define (the-sema m)<br> (cond ((eq? m 'release)<br> (mutex 'acquire)<br> (unless (= count maximal)<br> (set! count (+ 1 count)))<br>
(mutex 'release))<br> ((eq? m 'acquire)<br> (mutex 'acquire)<br> (cond<br> ((> count 0)<br> (set! count (- count 1))<br> (mutex 'release))<br>
(else<br> (mutex 'release)<br> (the-sema 'acquire))))<br> (else<br> (error "Unknown request -- " m))))<br> the-sema))<br><br>Thanks in advance.<br clear="all">
<br>-- <br>Thanks and Regards<br>Vinay Sachdev<br>