【SICP练习】111 练习3.24
2015-09-08 00:00
232 查看
练习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))
感谢访问,希望对您有所帮助。 欢迎关注或收藏、评论或点赞。
为使本文得到斧正和提问,转载请注明出处:
http://blog.csdn.net/nomasp
版权声明:本文为 NoMasp柯于旺 原创文章,未经许可严禁转载!欢迎访问我的博客:http://blog.csdn.net/nomasp
相关文章推荐
- Ruby中使用Block、Proc、lambda实现闭包
- Ruby中的block、proc、lambda区别总结
- C++实现的一个可以写递归lambda的Y函数
- C#特性之匿名方法和Lambda表达式
- 理解C#中的Lambda表达式
- 初步认识C#中的Lambda表达式和匿名方法
- C#3.0中Lambda表达式详解
- C#基础之Lambda表达式用法实例教程
- 深入理解Java中的Lambda表达式
- Java8新特性之Lambda表达式浅析
- Java Lambda表达式详解和实例
- Java函数式编程(一):你好,Lambda表达式
- 用Python编写一个简单的Lisp解释器的教程
- python中lambda与def用法对比实例分析
- Python lambda和Python def区别分析
- Python中的特殊语法:filter、map、reduce、lambda介绍
- 跟老齐学Python之大话题小函数(1)
- python基础教程之lambda表达式使用方法
- Python的lambda匿名函数的简单介绍
- 深入解析Python中的lambda表达式的用法