From 3074223314aa4655a09fb0654c83563782e6e7d2 Mon Sep 17 00:00:00 2001 From: Attila Lendvai Date: Mon, 16 Oct 2006 22:25:52 +0200 Subject: [PATCH] Added a simple gen-tree --- src/packages.lisp | 1 + src/random.lisp | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/packages.lisp b/src/packages.lisp index 747b094..c8f8cb2 100644 --- a/src/packages.lisp +++ b/src/packages.lisp @@ -53,6 +53,7 @@ #:gen-character #:gen-string #:gen-list + #:gen-tree #:gen-buffer #:gen-one-element ;; running tests diff --git a/src/random.lisp b/src/random.lisp index f91ca46..1052f35 100644 --- a/src/random.lisp +++ b/src/random.lisp @@ -235,6 +235,21 @@ producs objects." repeat (funcall length) collect (funcall elements)))) +(defun gen-tree (&key (size 20) + (elements (gen-integer :min -10 :max 10))) + "Returns a generator which producs random trees. SIZE control +the approximate size of the tree, but don't try anything above + 30, you have been warned. ELEMENTS must be a generator which +will produce the elements." + (labels ((rec (&optional (current-depth 0)) + (let ((key (random (+ 3 (- depth current-depth))))) + (cond ((> key 2) + (list (rec (+ current-depth 1)) + (rec (+ current-depth 1)))) + (t (funcall elements)))))) + (lambda () + (rec)))) + (defun gen-buffer (&key (length (gen-integer :min 0 :max 50)) (element-type '(unsigned-byte 8)) (elements (gen-integer :min 0 :max (1- (expt 2 8))))) -- 1.7.10.4