43 lines
795 B
Common Lisp
43 lines
795 B
Common Lisp
; quicksort
|
|
(defun quick-sort (l)
|
|
(if (<= (len l) 1)
|
|
l
|
|
(do
|
|
(define pivot (first l))
|
|
(+
|
|
(quick-sort (filter (lambda (n) (> pivot n)) l))
|
|
(list pivot)
|
|
(quick-sort (tail (filter (lambda (n) (<= pivot n)) l)))
|
|
))
|
|
))
|
|
|
|
; decrement a number
|
|
(defun dec (n) (- n 1))
|
|
|
|
; increment a number
|
|
(defun inc (n) (+ n 1))
|
|
|
|
; not a bool
|
|
(defun not (x) (if x 0 1))
|
|
|
|
; negate a number
|
|
(defun neg (n) (- 0 n))
|
|
|
|
; is a number positive?
|
|
(defun is-pos? (n) (> n 0))
|
|
|
|
; is a number negative?
|
|
(defun is-neg? (n) (< n 0))
|
|
|
|
; return second element of list
|
|
(defun second (l) (index l 1))
|
|
|
|
; return third element of list
|
|
(defun third (l) (index l 2))
|
|
|
|
; return fourth element of list
|
|
(defun fourth (l) (index l 3))
|
|
|
|
|
|
|