36 lines
730 B
Common Lisp
36 lines
730 B
Common Lisp
; quicksort
|
|
(defun qs (l)
|
|
(if (<= (len l) 1)
|
|
l
|
|
(do
|
|
(define pivot (first l))
|
|
(+
|
|
(qs (filter (lambda (n) (> pivot n)) l))
|
|
(list pivot)
|
|
(qs (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))
|
|
|
|
; define a function `fact` that takes an argument `n`
|
|
(defun fact (n)
|
|
(if (<= n 1)
|
|
1
|
|
(* n (fact (- n 1)))
|
|
))
|
|
|
|
(print "example.lisp loaded") |