Skip to content

Instantly share code, notes, and snippets.

@cametan001
Created June 15, 2015 11:33
Show Gist options
  • Save cametan001/84e44b10930be5a2cc38 to your computer and use it in GitHub Desktop.
Save cametan001/84e44b10930be5a2cc38 to your computer and use it in GitHub Desktop.
(require srfi/1)
(define (make-table)
(let loop ((local-table `(*table*)))
(letrec ((lookup
(lambda (key-1 key-2)
(let ((subtable (assoc key-1 (cdr local-table))))
(when subtable
(let ((record (assoc key-2 (cdr subtable))))
(when record
(cdr record)))))))
(insert
(lambda (key-1 key-2 value)
(let ((head (car local-table))
(subtable (assoc key-1 (cdr local-table))))
(if subtable
(let ((record (assoc key-2 (cdr subtable))))
(loop (cons head
(alist-cons
key-1
(alist-cons key-2 value
(if record
(alist-delete
key-2
(cdr subtable))
(cdr subtable)))
(alist-delete key-1 (cdr local-table))))))
(loop (cons head (cons `(,key-1 ,(cons key-2 value))
(cdr local-table))))))))
(dispatch
(lambda (m)
(case m
((lookup-proc) lookup)
((insert-proc) insert)
(else (error "Unknown operation -- TABLE" m))))))
dispatch)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment