;;; This is a useful collection of LISP tools.

(in-package 'USER)

(defun fact (n)
  (fact-help n 1))

(defun fact-help (n accum)
  (if (<= n 1) accum
    (fact-help (1- n) (* n accum))))


(defun n-choose-k (n k)
  (when (<= 0 k n)
	(/ (fact n)
	   (* (fact k)
	      (fact (- n k))))))

(defun print-bin (binary-num)
  (format t "~16,'0B~%" binary-num))


(defun sum-vect (vect num &optional (index 0))
  (let ((tot 0))
    (dotimes (i (- num index) tot)
       (setf tot (+ tot (svref vect (+ i index)))))))

(defun dot-prod (v1 v2 len)
  (let ((tot 0))
    (dotimes (i len tot)
       (setf tot (+ tot
		    (* (svref v1 i)
		       (svref v2 i)))))))

(defun mix-array (an-array start-index end-index)
  (do* ((i start-index (1+ i))
	(range (- end-index start-index))
	(j (+ start-index (random range)) (+ start-index (random range)))
	(temp 0))
      ((>= i end-index))
      (setf temp (svref an-array i))
      (setf (svref an-array i) (svref an-array j))
      (setf (svref an-array j) temp)))

