SICP sicp 111: 3.24 exercises 1113.24 and

Source: Internet
Author: User

SICP sicp 111: 3.24 exercises 1113.24 and
Exercise 3-24 original

Exercise 3.24. 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 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 "integrity" of keys. Make-table shocould return a dispatch procedure that can be used to access appropriate lookup and insert! Procedures for a local table.

Code
(define (make-table same-key?)    (let ((local-table (list '*table*)))       (define (lookup key-1 key-2)          (let ((subtable (assoc key-1 (cdr local-table))))             (if subtable                 (let ((record (assoc key-2 (cdr subtable)))                    (if record                        (cdr record)                        false))                false)))      (define (insert! key-1 key-2 value)          (let ((subtable (assoc key-1 (cdr local-table))))             (if subtable                 (let ((record (assoc key-2 (cdr subtable))))                      (if record                          (set-cdr! record value)                          (set-cdr! subtable                                    (cons (key-2 value)                                          (cdr subtable)))))                    (set-cdr! local-table                              (cons (list key-1                                          (cons key-2 value))                                    (cdr local-table)))))            'ok)        (define (assoc key records)            (cond ((null? records)                    false)                  ((same-key? key (caar records))                      (car records))                  (else                    (assoc key (cdr records)))))              (define (dispatch m)            (cond ((eq? m 'lookup-proc) lookup)                  ((eq? m 'insert-proc!) insert!)                  (else                     (error "Unknown operation -- TABLE" m))))        dispatch))

For this article to get an axe and a question, please indicate the source:
Http://blog.csdn.net/nomasp

Email and Skype: nomasp@outlook.com
Facebook: https://www.facebook.com/yuwang.ke
CSDN blog: http://blog.csdn.net/nomasp
Sina Weibo: http://weibo.com/nomasp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.