zoukankan      html  css  js  c++  java
  • 【SICP练习】111 练习3.24

    练习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柯于旺 原创文章,如需转载请联系本人。

  • 相关阅读:
    elementui中el-alert实现换行的方法
    Element-UI中el-table实现简单动态数据绑定的实现
    腾讯课堂多倍速、去除xxx正在观看
    Linux vim 常用操作
    尚硅谷《谷粒商城项目总结》
    Centos 安装 Harbor
    centos 7 安装 kubernetes
    Mac VirtualBox 没有 Host-Only 选项怎么设置?
    VirtualBox 安装 centos7
    ssh 使用 sed 替换的时候,替换的字符串有单双引号的时候怎么用
  • 原文地址:https://www.cnblogs.com/NoMasp/p/4786093.html
Copyright © 2011-2022 走看看