Skip to content

Instantly share code, notes, and snippets.

@krono
Created October 28, 2015 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krono/7db9b48e31db23664c1b to your computer and use it in GitHub Desktop.
Save krono/7db9b48e31db23664c1b to your computer and use it in GitHub Desktop.
--- binarytrees-generic.rkt 2015-10-28 11:06:42.000000000 +0100
+++ binarytrees-generic-boolean.rkt 2015-10-28 11:51:04.000000000 +0100
@@ -14,15 +14,15 @@
(define-syntax leaf? (make-rename-transformer #'*leaf?))
(define-syntax node (make-rename-transformer #'*node))
(define-syntax node? (make-rename-transformer #'*node?))
-(define-syntax-rule (leaf-val l) (*leaf-val l))
+(define-syntax-rule (leaf-val l) (if (*leaf-val l) 0 1))
(define-syntax-rule (node-left n) (*node-left n))
(define-syntax-rule (node-right n) (*node-right n))
(define (make item d)
(if (= d 0)
- (leaf item)
- (let ([item2 (* item 2)] [d2 (- d 1)])
- (node item (make (- item2 1) d2) (make item2 d2)))))
+ (leaf item)
+ (let ([item2 (not item)] [d2 (- d 1)])
+ (node item (make item2 d2) (make item2 d2)))))
(define (check t)
(let loop ([t t] [acc 0])
@@ -39,7 +39,7 @@
(let ([stretch-depth (+ max-depth 1)])
(printf "stretch tree of depth ~a\t check: ~a\n"
stretch-depth
- (check (make 0 stretch-depth))))
+ (check (make #f stretch-depth))))
(let ([long-lived-tree (make 0 max-depth)])
(for ([d (in-range 4 (+ max-depth 1) 2)])
(let ([iterations (expt 2 (+ (- max-depth d) min-depth))])
@@ -47,8 +47,8 @@
(* 2 iterations)
d
(for/fold ([c 0]) ([i (in-range iterations)])
- (+ c (+ (check (make i d))
- (check (make (- 0 i) d))))))))
+ (+ c (+ (check (make #t d))
+ (check (make #f d))))))))
(printf "long lived tree of depth ~a\t check: ~a\n"
max-depth
(check long-lived-tree)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment