Commit da91d47e authored by Juergen Nickelsen's avatar Juergen Nickelsen

removed suplus )'s after incf&decf; sort done right now

parent a2ff7909
......@@ -334,29 +334,39 @@
((listp sequence) (null sequence))
(t (error "empty: not a sequence: %s" sequence))))
(defun sort (l pred)
(defun sort (s pred)
"sort sequence S with predicate PRED and return the result"
(cond ((stringp s)
(apply #'string (sort-list (split-string s "") pred)))
((listp s) (sort-list s pred))
(t (error "sort: not a sequence: %s" sequence))))
(defun sort-list (l pred)
"sort list L with predicate PRED and return the resulting list"
(if (or (empty l) (empty (rest l)))
(if (or (null l) (null (cdr l)))
l
(let* ((len (length l))
(first (div len 2))
(l1 (subseq l 0 first))
(l2 (subseq l first))
(l1 (sublist l 0 first))
(l2 (sublist l first))
(merge (lambda (l1 l2)
(if (empty l1)
(if (null l1)
l2
(if (empty l2)
(if (null l2)
l1
(if (pred (first l1) (first l2))
(prepend (first l1) (merge (rest l1) l2))
(prepend (first l2) (merge l1 (rest l2)))))))))
(merge (sort l1 pred) (sort l2 pred)))))
(if (pred (car l1) (car l2))
(cons (car l1) (merge (cdr l1) l2))
(cons (car l2) (merge l1 (cdr l2)))))))))
(merge (sort-list l1 pred) (sort-list l2 pred)))))
(defun make-string (n el)
"return a string of N occurences of element EL"
"return a string of N occurences of element EL (may be a function)"
(if (zerop n)
""
(concat el (make-string (1- n) el))))
(concat (if (functionp el)
(el)
(string el))
(make-string (1- n) el))))
(defun join (sep &rest args)
"append all ARGS (or their elements) to a string, separated by SEP"
......@@ -377,9 +387,9 @@
(defun 2+ (n) (+ n 2))
(defun 2- (n) (- n 2))
(defspecial incf (var &optional incr)
(set var (eval (list '+ var (or incr 1))))))
(set var (eval (list '+ var (or incr 1)))))
(defspecial decf (var &optional decr)
(set var (eval (list '- var (or decr 1))))))
(set var (eval (list '- var (or decr 1)))))
(defun fib (n)
(if (< n 2)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment