博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【SICP练习】111 练习3.24
阅读量:6405 次
发布时间:2019-06-23

本文共 1703 字,大约阅读时间需要 5 分钟。

练习3-24

原文

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 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.

代码

(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))



感谢访问,希望对您有所帮助。 欢迎关注或收藏、评论或点赞。


为使本文得到斧正和提问,转载请注明出处:


版权声明:本文为 NoMasp柯于旺 原创文章,如需转载请联系本人。

转载于:https://www.cnblogs.com/NoMasp/p/4786093.html

你可能感兴趣的文章
基础数据类型之字典
查看>>
第七次作业
查看>>
Oracle中NVARCHAR2与VARCHAR2的区别
查看>>
php debug
查看>>
新的一天,新的开始。
查看>>
<img> <a> CSS
查看>>
JS call和apply方法使用
查看>>
使用JavaScript设置、获取父子页面中的值
查看>>
在vue中如何实现购物车checkbox的三级联动
查看>>
华为C8812 手机连接电脑的问题
查看>>
Python3-装饰器
查看>>
读《More Effective C++中文版》笔记
查看>>
Elementui实战知识点随记
查看>>
Oracle10g RAC 修改IP [转载]
查看>>
关于char p[] = "hello world"; char *p = "hello world";
查看>>
FIREDAC的心得
查看>>
NPOI批量导入大量数据
查看>>
了解 Windows Azure 存储计费 – 带宽、事务和容量
查看>>
mysql5.7.22 zip 版安装
查看>>
【题解】最大公约数之和 V3 51nod 1237 杜教筛
查看>>