126 lines
2.7 KiB
Common Lisp
126 lines
2.7 KiB
Common Lisp
|
|
; not a bool
|
|
(defun not (x) (if x 0 1))
|
|
|
|
; logical and
|
|
(defun and (a b) (if a (if b 1 0) 0))
|
|
|
|
; logical or
|
|
(defun or (a b) (if a 1 (if b 1 0)))
|
|
|
|
|
|
|
|
; 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))
|
|
|
|
; decrement a number
|
|
(defun dec (n) (- n 1))
|
|
|
|
; increment a number
|
|
(defun inc (n) (+ n 1))
|
|
|
|
|
|
; pad string on the end
|
|
(defun string-rpad (str length pad_char)
|
|
(string-pad str length pad_char "rpad"))
|
|
|
|
; pad string on the begining
|
|
(defun string-lpad (str length pad_char)
|
|
(string-pad str length pad_char "lpad"))
|
|
|
|
|
|
|
|
|
|
; 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))
|
|
|
|
; return 1 when list contains item otherwise 0
|
|
(defun member (lst itm)
|
|
(do
|
|
; TODO check if is empty list
|
|
(define found_index -1)
|
|
(define i 0)
|
|
(define lst_len (len lst))
|
|
|
|
(while (and (< i lst_len) (= found_index -1))
|
|
(if (= itm (index lst i))
|
|
(define found_index i)
|
|
(define i (+ i 1))
|
|
))
|
|
|
|
; TODO when nil will be implemented itm / nil
|
|
(if (!= -1 found_index)
|
|
1
|
|
0)
|
|
))
|
|
|
|
(defun make-list-of (size val)
|
|
(do
|
|
(define lst '())
|
|
(define i 0)
|
|
(while (< i size)
|
|
(define lst (push lst val))
|
|
(define i (inc i)))
|
|
lst
|
|
))
|
|
|
|
(defun make-list (size)
|
|
(make-list-of size nil))
|
|
|
|
|
|
; quicksort
|
|
(defun quick-sort-by (l cmp)
|
|
(if (<= (len l) 1)
|
|
l
|
|
(do
|
|
(define pivot (first l))
|
|
(+
|
|
(quick-sort-by (filter (lambda (n) (cmp pivot n)) l) cmp)
|
|
(list pivot)
|
|
(quick-sort-by (tail (filter (lambda (n) (not (cmp pivot n))) l)) cmp)
|
|
))
|
|
))
|
|
|
|
(defun quick-sort (l)
|
|
(quick-sort-by l (lambda (a b) (> a b))))
|
|
|
|
|
|
|
|
|
|
; 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
|
|
))
|