; getAt(list index) ; ; returns the element in the list at the given index by ; recursively cutting the list and decrementing the index ; until it is zero ; ; ex. ; getAt ( '(1 3 6) 0 ) returns 1 ; getAt ( '(1 3 6) 1 ) returns 3 ; (defun getAt (list index) (cond ((= index 0) (car list)) (t (getAt (cdr list) (- index 1))) ) ) ; insert(pre item post) ; ; inserts an item into a list recursively, given a list ; list that is assumed to precede the given item--post ; is assumed to be the rest of the list ; ; it is assumed that the new item is supposed to go ; either between pre and post, somewhere in post, ; or after post ; (defun insert (pre item post) (cond ; if post is null, then we've reached the end ; of the list, and we need to add item to the end ((null post) (append pre (list item))) ; if the next item in post precedes item in ; order, then recursively call insert, with ; the next item in post removed and added to ; pre ((> (car post) item) (insert (append pre (list (car post))) item (cdr post) ) ) ; otherwise, we know that the item belongs ; right here between pre and post (t (append (append pre (list item)) post)) ) ) ; insertstr (pre item post) ; ; same as insert() except for strings ; ; uses "string<" instead of just "<" ; (defun insertstr (pre item post) (cond ((null post) (append pre (list item))) ((string> (car post) item) (insert (append pre (list (car post))) item (cdr post) ) ) (t (append (append pre (list item)) post)) ) ) ; addToList (item list) ; ; adds a new item to list in descending order ; ; it is assumed that the items in list ; are already sorted in descending order ; (defun addToList (item list) (insert '() item list) ) ; addStrToList (item list) ; ; same as addToList() except for strings ; (defun addStrToList (item list) (insert '() item list) ) Screen capture of run of addToLis > (addToList 1 '()) (1) > (addToList 1 '(2)) (2 1) > (addToList 1 '(-3)) (1 -3) > (addToList 5 '(25 15 10)) (25 15 10 5) > (addToList 5 '(4 3 2 1)) (5 4 3 2 1) > (addToList 5 '(15 10 0 -5)) (15 10 5 0 -5) >