;;; Dan Mead ;;; csc345 ;;; 2/29/2005 ;;; functions for assignment are sum, add-1-all, binarysize, my-replace, t-reverse ;; sum function ;; n: integer, m: integer ;; returns the sum of n and m (defun sum(n m) "sum checks if n and m are intergers, then finds their sum" (cond ((not (numberp n)) nil) ((not (numberp m)) nil) ((= m 0) n) ((> m 0) (sum(1+ n)(1- m))) ((< m 0) (sum(1- n)(1+ m))))) ;;; add-1-all function ;;; L: list of ints ;;; returns the list of ints incremented by one, nil if there are ;;; any non ints in the list (defun list-is-ints (L) "check fuction for add-1-all, ensures the list is correctly formed" (cond ((not (listp L)) nil) ((endp L) t) ((not (numberp (first L))) nil) (t (list-is-ints (rest L))))) (defun add-1-all-aux(L) "adds one to a list of ints" (cond ((endp L) nil) (t (cons (1+ (first L)) (add-1-all-aux (rest L)))))) (defun add-1-all (L) "adds one to a list of integers, returns nil if it's a bad list" (cond ((not (list-is-ints L)) nil) (t (add-1-all-aux L)))) ;; binaryize function ;;; n: a non negative integer ;;; returns the binary list of n (defun binaryize(n) "returns the non negative number N in binary form" (cond((< n 0) nil) ((= n 0) 0) ((= n 1) (list (rem n 2))) (t (append (binaryize (floor n 2)) (list(mod n 2)))))) ;;; my-replace function ;;; e1: an element e2: an element L: a list of elements ;;; returns the updated list L with all e1 replaced with (defun my-replace (e1 e2 L) "replaces element e1 contained in list L with element e2" (cond ((endp L) nil) ((equal (first L) e1) (cons e2 (my-replace e1 e2 (rest L)))) ((atom(first L))(cons (first L) (my-replace e1 e2 (rest L))) ) (t (cons (my-replace e1 e2 (first L)) (my-replace e1 e2 (rest L))) ))) ;;; function t-reverse ;;; L; a list of elements, acc: an accumulator list ;;; returns the list L in reverse order via acc (defun rev-aux(L acc) "aux function for t-reverse" (cond ((endp L) acc) (t (rev-aux (rest L) (cons (first L) acc))))) (defun t-reverse (L) "returns the list L in shallow reverse order, using tail recursion" (rev-aux L nil))