mlisp/stdlib/stdlib.lisp

69 lines
1.5 KiB
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))
; return fifth element of list
(defun fifth (l) (index l 4))
; from list of lists creates csv string
; (print (make-csv '(("r1c1" "r1c2") ("r2c1" "r2c2"))))
(defun make-csv (csv_list)
(do
(define rows_str "")
(define r 0)
(for row csv_list
(define cols_str "")
(define c 0)
(for col row
(if (= c 0)
(define cols_str col)
(define cols_str (+ cols_str "," (display col))))
(define c (inc c)))
(if (= r 0)
(define rows_str cols_str)
(define rows_str (+ rows_str "\n" cols_str)))
(define r (inc r)))
rows_str
))
(defun setq (var val) (define var val))