Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 834746da09
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 32 lines (24 sloc) 0.845 kb
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
;(use srfi-34)

(define (memo-tree-add! tree path value)
  (if (null? (cdr path))
      (hash-table-put! tree (car path) value)
      (let ((new-tree (make-hash-table)))
          (hash-table-put! tree (car path) new-tree)
          (memo-tree-add! new-tree (cdr path) value))))

(define (make-memo-tree) (make-hash-table))

(define (memo-tree-ref tree path)
  (if (null? (cdr path))
      (hash-table-get tree (car path))
      (let ((sub-tree (hash-table-get tree (car path))))
        (memo-tree-ref sub-tree (cdr path)))))


(define (test)
  (let ((t (make-memo-tree)))
    (memo-tree-add! t '(a b c) "abc")
    (memo-tree-add! t '(d) "d")

    (print (memo-tree-ref t '(d)))
    (print (memo-tree-ref t '(a b c)))
    (print (guard (e (else 'ok))
                  (print (memo-tree-ref t '(a b d)))))
    ))

(define (main args)
  (test))
Something went wrong with that request. Please try again.