Lisp Quicksort Function by Shane Zentz



; Shane Zentz
; C-311
; homework # 7

(defun plus (&rest Z)
"This function adds only the numbers in a list..."
(setq p 0)
(dolist (element Z p)
(if (numberp element)
(setq p (+ p element))))p)
plus

(plus 100 100 100 'l)
300

(plus 8 5 3 4 2)
22

(plus 'a 'b 'c 'd)
0

(setq G 'plus)
plus

(funcall G 9 1 2 8)
20

(apply 'plus '(g h 8 i 0 kn 2))
10

(setq c '([19 5 64 96 2 19 48 1]))
([19 5 64 96 2 19 48 1])

(kwiksort c 0 7)
-1 ;not working

(defun kwiksort (X left right)
(if (< left right)
(setq split (partition X left right))
(kwiksort X left split)
(kwiksort X (+ 1 split) right)))
kwiksort

;;partition function for the kwiksort
(defun partition (Y left right)
(setq val left)
(setq lm (- left 1))
(setq rm (+ right 1))
(while (and (> rm val) (< lm val))
(- rm 1)(+ lm 1)
(if (< lm rm)
(setq temp rm rm lm lm temp)))rm)
partition

(defun count-val (L val R)
"This function returns the total occurences of val in the list L"
(while (> (length L) 1)
(if (equal (car L) val)
(setq R (+ R 1)))
(setq L (cdr L)))R)
count-val

(setq Y '(a b d a o f a k))
(a b d a o f a k)

(count-val '(a b c d e a g a p) 'a 0)
3

(count-val Y 'a 0)
3