; hw2.lisp ; CS 381 homework 2 ; Jacob Lundberg ; ADD2 returns 2 plus op1. (defun ADD2 (op1) (cond ((eql nil op1) 2) (T (+ 2 op1)) ) ) ; THREE-LAST returns the last element of a list of three elements. (defun THREE-LAST (list1) (cond ((< (list-length list1) 3) nil) (T (car (cddr list1))) ) ) ; THREEA returns ((nil 400 . 400) 500). (defun THREEA () (cons (cons nil (cons 400 400)) (cons 500 nil)) ) ; THREEB returns (30 (10 . #\b)). (defun THREEB () (cons 30 (cons (cons 10 #\b) nil)) ) ; MY-NTHCDR returns the n'th cdr of a list. (defun MY-NTHCDR (n1 list1) (cond ((null list1) nil) ; no need to take cdr of nil ((< n1 0) nil) ; the real nthcdr would give an error ((= n1 0) list1) ; whup, we're done (T (MY-NTHCDR (- n1 1) (cdr list1))) ) ) ; MY-MEMBER returns a sub-list starting at a member. (defun MY-MEMBER (mem1 list1) (cond ((null list1) nil) ((eq mem1 (car list1)) list1) (T (MY-MEMBER mem1 (cdr list1))) ) ) ; MY-UNION returns a set that is the union of the given sets, unordered. ; *NOTE* that if you don't give it true sets it may not behave like UNION. (defun MY-UNION (set1 set2) (cond ((null set2) set1) ((MY-MEMBER (car set2) set1) (MY-UNION set1 (cdr set2))) (T (MY-UNION (cons (car set2) set1) (cdr set2))) ) ) ; MY-SUBST returns a tree with one value replaced by another. (defun MY-SUBST (new1 old1 tree1) (cond ((null tree1) nil) ((not (listp tree1)) (if (eq tree1 old1) new1 tree1)) ((eq (car tree1) old1) (cons new1 (MY-SUBST new1 old1 (cdr tree1)))) ((eq (cdr tree1) old1) (cons (MY-SUBST new1 old1 (car tree1)) new1)) (T (cons (MY-SUBST new1 old1 (car tree1)) (MY-SUBST new1 old1 (cdr tree1)))) ) ) ; NUMBERS-FIRST returns a list or tree with all the numbers at the front. (defun NUMBERS-FIRST (list1) (cond ((null list1) nil) ((numberp (car list1)) (cons (car list1) (NUMBERS-FIRST (cdr list1)))) (T (append (NUMBERS-FIRST (cdr list1)) (cons (car list1) nil))) ) ) ; FLAT-NUMBERS-FIRST returns a list with all the numbers at the front. (defun FLAT-NUMBERS-FIRST (list1) (cond ((null list1) nil) ((listp (car list1)) (FLAT-NUMBERS-FIRST (append (car list1) (cdr list1)))) ((numberp (car list1)) (cons (car list1) (FLAT-NUMBERS-FIRST (cdr list1)))) (T (append (FLAT-NUMBERS-FIRST (cdr list1)) (cons (car list1) nil))) ) )