First cut of 3.25
For now only lookup takes a list of indices. Insert! is hardcoded to two indices.
This commit is contained in:
parent
d1ffdff403
commit
2f2e704cf6
|
@ -0,0 +1,52 @@
|
|||
#lang sicp
|
||||
|
||||
(#%require racket/trace)
|
||||
|
||||
(define (make-table)
|
||||
(list '*table*))
|
||||
|
||||
;;(define (find-record
|
||||
|
||||
(define (assoc key records)
|
||||
(cond ((null? records) false)
|
||||
((equal? key (caar records)) (car records))
|
||||
(else (assoc key (cdr records)))))
|
||||
|
||||
(define (table? l)
|
||||
(pair? (cdr l)))
|
||||
|
||||
(trace-define (lookup keys table)
|
||||
(cond ((null? keys) (cdr table))
|
||||
((not (table? table)) false)
|
||||
(else
|
||||
(let ((subtable (assoc (car keys) (cdr table))))
|
||||
(if subtable
|
||||
(lookup (cdr keys) subtable)
|
||||
false)))))
|
||||
|
||||
(define (insert! key-1 key-2 value table)
|
||||
(let ((subtable (assoc key-1 (cdr table))))
|
||||
(if subtable
|
||||
(let ((record (assoc key-2 (cdr subtable))))
|
||||
(if record
|
||||
(set-cdr! record value)
|
||||
(set-cdr! subtable
|
||||
(cons (cons key-2 value)
|
||||
(cdr subtable)))))
|
||||
(set-cdr! table
|
||||
(cons (list key-1
|
||||
(cons key-2 value))
|
||||
(cdr table)))))
|
||||
'ok)
|
||||
|
||||
(#%require (only racket/base module+))
|
||||
|
||||
(module+ test
|
||||
(#%require rackunit)
|
||||
|
||||
(test-begin
|
||||
(define t (make-table))
|
||||
(insert! 10 10 'a t)
|
||||
(check-equal? (lookup '(10 10) t) 'a)
|
||||
(check-equal? (lookup '(10 10 10) t) false))
|
||||
)
|
Loading…
Reference in New Issue