diff options
-rw-r--r-- | utilities.lisp | 35 |
1 files changed, 22 insertions, 13 deletions
diff --git a/utilities.lisp b/utilities.lisp index 69ddcfe..525e2c1 100644 --- a/utilities.lisp +++ b/utilities.lisp @@ -13,7 +13,7 @@ host (get-universal-time)))))) -(defun take (n list &key share-tail) +(defun take (n list &optional share-tail) "Returns two values. The first value returned is a list of the first N members of LIST. The second value is a list of the remaining values of list. @@ -34,20 +34,35 @@ tail (copy-seq tail))))))) -(defun insert-nth (x n list) +(defun insert-nth (x n list &optional share-tail) "Creates a new list, the result of inserting X into the Nth position of LIST, displacing the rest of the elements by one position. If N is greater than the length of LIST, X becomes the last element of - LIST. If N is negative, the element is inserted from the back of + LIST. + + If N is negative, the element is inserted from the back of the list. If the abslute value of -N is greater than the lenght of the list, a list just containing X is returned." (when (minusp n) (setf n (+ 1 n (length list)))) - (multiple-value-bind (front back) (take n list) - ;; NCONC ok b/c this call to TAKE returns values that do not shre - ;; memory with LIST + (multiple-value-bind (front back) (take n list share-tail) (nconc front (cons x back)))) +(defun remove-nth (n list &optional share-tail) + "Removes Nth member of list. Returns two values. + + The first value is the new list with the Nth member removed. The + second value is the removed item. + + See insert-nth for a description of the behavior of negative values + of N, and for documentation on SHARE-TAIL." + (when (minusp n) + (setf n (+ n (length list)))) + (multiple-value-bind (front back) (take n list share-tail) + (values + (nconc front (cdr back)) + (car back)))) + (defun nswap (list n m) "Swap Nth and Mth members of LIST. Mutates LIST. Assumes both N and M are less than the length of LIST." @@ -56,10 +71,4 @@ (nth m list) tmp) list)) -(defun pop-nth (n list &optional share-tail) - (when (minusp n) - (setf n (+ n (length list)))) - (multiple-value-bind (front back) (take n list share-tail) - (values - (nconc front (cdr back)) - (car back)))) + |