Permalink
Browse files

added memo-tree

  • Loading branch information...
1 parent a80c04a commit 834746da090414f8a2af31c6915844dce4733cbd Toru Hisai committed Apr 23, 2012
Showing with 31 additions and 0 deletions.
  1. +31 −0 memo-tree.scm
View
@@ -0,0 +1,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))

0 comments on commit 834746d

Please sign in to comment.