-
Notifications
You must be signed in to change notification settings - Fork 0
/
table-as-function.scm
56 lines (50 loc) · 1.58 KB
/
table-as-function.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
;; A multi dimensional table
(define (make-table same-key?)
(let ((local-table (list 'table)))
(define (assoc key records)
(cond ((null? records) false)
((same-key? key (caar records)) (car records))
(else (assoc key (cdr records)))))
(define (_lookup keys table)
(let ((subtable (assoc (car keys)
(cdr table))))
(if subtable
(cond ((null? (cdr keys)) subtable) ;; Just found the final key, return the subtable
((list? (cdr table)) ;; Has further subtables to search
(_lookup (cdr keys) subtable)))
false)))
(define (lookup keys)
(if (null? keys)
false
(let ((record (_lookup keys local-table)))
(if record
(cdr record)
false))))
(define (_insert! keys value table)
(let ((subtable (assoc (car keys) (cdr table))))
(if subtable
(cond ((null? (cdr keys))
(set-cdr! subtable value))
((not (list? (cdr subtable)))
(set-cdr! subtable
(list (cons (caar keys) ())))
(_insert! (cdr keys) value subtable))
(else (_insert! (cdr keys) value subtable)))
(cond ((null? (cdr keys))
(set-cdr! table
(cons (cons (car keys) value)
(cdr table))))
(else (begin (set-cdr! table
(cons (cons (car keys) ())
(cdr table)))
(_insert! keys value table))))))
'ok)
(define (insert! keys value)
(if (null? keys)
'ok
(_insert! keys value local-table)))
(define (dispatch m)
(cond ((eq? m 'lookup) lookup)
((eq? m 'insert!) insert!)
(else (error "Unknown operation on TABLE\n"))))
dispatch))