[plt-scheme] Doubt in SICP Exercise-3.47

From: Vinay Sachdev (vinay.sachdev at gmail.com)
Date: Sat May 16 03:18:33 EDT 2009

Hi,
 I have doubt in SICP exercise 3.47. I have found two  solutions
 but totally different. Can somebody please tell me which is
 correct solutions and why?
 I am pasting both the solutions below along with the link,
1)
http://git.wizardbook.org/wizardbook/tree/3.4.2/make-mutex-semaphore.scm

;; May be cheating in the sense that make-non-blocking-mutex is a thin
;; wrapper around test-and-set!.
(define (make-mutex-semaphore n)
  (define (make-mutices n)
    (let make-mutices ((i 0)
                       (mutices '()))
      (if (< i n)
          (make-mutices (+ i 1) (cons (make-non-blocking-mutex) mutices))
          mutices)))
  (let ((mutices (make-mutices n)))
    (define (acquire)
      (let ((acquirendum
             (find-matching-item mutices
                                 (lambda (mutex) (false? (mutex
'acquire))))))
        (if (not acquirendum) (acquire))))
    (define (release)
      (let ((relaxandum
             (find-matching-item mutices
                                 (lambda (mutex) (mutex 'release)))))
        (if relaxandum
            (relaxandum 'release)
            (error "No acquired mutices -- RELEASE" mutices))))
    (define (dispatch m)
      (cond ((eq? m 'acquire) (acquire))
            ((eq? m 'release) (release))
            (else (error "Unknown request" m))))
    dispatch))

2)
http://eli.thegreenplace.net/2007/10/26/sicp-334/

(define (make-semaphore-mtx maximal)
  (let ((count maximal)
        (mutex (make-mutex)))
    (define (the-sema m)
      (cond ((eq? m 'release)
              (mutex 'acquire)
              (unless (= count maximal)
                (set! count (+ 1 count)))
              (mutex 'release))
            ((eq? m 'acquire)
              (mutex 'acquire)
              (cond
                ((> count 0)
                  (set! count (- count 1))
                  (mutex 'release))
                (else
                  (mutex 'release)
                  (the-sema 'acquire))))
            (else
              (error "Unknown request -- " m))))
    the-sema))

Thanks in advance.

-- 
Thanks and Regards
Vinay Sachdev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20090516/a3028afe/attachment.html>

Posted on the users mailing list.