[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug in reference implementation of HASH-TABLE-UPDATE!



Quoth the SRFI document:

   Procedure: hash-table-update! hash-table key function [ thunk ]
     ---> undefined

   Semantically equivalent to, but may be implemented more efficiently
   than, the following code:

   (hash-table-set! hash-table key
                    (function (hash-table-ref hash-table key thunk)))

But after loading the reference implementation into my friendly
neighbourhood Scheme REPL:

(let ()
  (define (test procedure)
    (let ((ht (make-hash-table)))
      (hash-table-set! ht 0 'fnord)
      (procedure ht)
      (hash-table-ref ht 0 (lambda () #f))))
  (define (update-procedure ht)
    (lambda (x)
      x                               ;ignore
      (hash-table-delete! ht 0)
      'foobar))
  (define (do-update ht)
    (hash-table-update! ht 0 (update-procedure ht) (lambda () 'lose)))
  (define (do-ref-and-set ht)
    (hash-table-set!
     ht
     0
     ((update-procedure ht) (hash-table-ref ht 0 (lambda () 'lose)))))
  (list (test do-update)
        (test do-ref-and-set)))
;Value: (#f foobar)

So it appears that the two expressions, one using HASH-TABLE-UPDATE!
and the other using HASH-TABLE-REF and HASH-TABLE-SET!, are not
equivalent in the reference implementation.