(defun at (name in) (gethash name in)) (defun add (name value in) (setf (gethash name in) value)) (defun zap (in) (dohash (key value in) (setf (node-got value) nil))) (defparameter *wme* nil) (defun worker (fn &optional (*wme* *wme*) (min 0) (max 1) (bins 10) (normalize t)) (underling fn min max bins normalize)) (defun underling (fn min max bins normalize) (setf *wme* (make-instance 'wme :min min :max max :bins bins :normalize normalize))) (funcall fn))) (defmethod got ((n node)) (or (node-got n) (flat))) (defmethod got1 ((n notv)) (let ((kid (first (mapcar #'got (nodes-kids n))))) `(,(first kid) . ,(1- (rest kid))))) (defmethod got1 ((n nodes)) (let ((conjoin (combine n)) (kids (mapcar #'got (nodes-kids n))) out) (dolist (one (transpose kids) (normalize (transpose out))) (let ((what (first (first one))) (combined (funcall conjoin one #'rest))) (push (cons what combined) out))))) (defun normalize (l) (let ((sum 0)) (dolist (x l) (incf sum (rest x))) (mapcar #'(lambda (one) `(,(first one) . ,(/ (rest one) sum))) l))) (deftest test-normalize () (let ((l '((a . 1) (b . 2) (c . 3)))) (check (equalp '((a . 1/6) (b . 1/3) (c . 1/2)) (normalize l))))) (defun flat (&optional (bins (opt-bins *wme*))) (let (out) (dotimes (bin bins (reverse out)) (push `(,bin . ,(/ 1 bins)) out)))) (defun test-flat () (check (equalp (flat 3) '((0 . 1/3) (1 . 1/3) (2 . 1/3)))))