A table constructor make-table

In the table implementations above, the keys are tested for equality using equal? (called by assoc). This is not always the appropriate test. For instance, we might have a table with numeric keys in which we don't need an exact match to the number we're looking up, but only a number within some tolerance of it. Design a table constructor make-table, that takes as an argument a same-key? procedure that will be used to test ''equality'' of keys. Make-table should return a dispatch procedure that can be used to access appropriate lookup and insert! procedures for a local table.

There are no comments yet.

Authentication required

You must log in to post a comment.

(define table-equal (make-table equal?))
(define get-equal (table-equal 'lookup-proc))
(define put-equal (table-equal 'insert-proc!))

(put-equal 1 2 42)
(check-equal? (get-equal 1 2) 42)
(check-false (get-equal 1 2.0000001))

(define (same-key-epsilon? epsilon)
  (lambda (x y) 
      (if (< (abs (- x y)) epsilon) #t

(define table-epsilon (make-table (same-key-epsilon? 0.1)))
(define get-epsilon (table-epsilon 'lookup-proc))
(define put-epsilon (table-epsilon 'insert-proc!))

(put-epsilon 1 2 42)
(check-equal? (get-epsilon 1 2) 42)
(check-equal? (get-epsilon 1 2.001) 42)
(check-equal? (get-epsilon 1.01 2.01001) 42)
(check-false (get-epsilon 1.2 2.1))