; hw4.lisp ; CS 381 homework 4 ; Jacob Lundberg ; 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-SET-UNION returns a set that is the union of two sets, unordered. ; *NOTE* that if you don't give it true sets it may not behave like UNION. (defun MY-SET-UNION (set1 set2) (cond ((null set2) set1) ((MY-MEMBER (car set2) set1) (MY-SET-UNION set1 (cdr set2))) (T (MY-SET-UNION (cons (car set2) set1) (cdr set2))) ) ) ; MY-SUB-UNION does the work for MY-UNION, which is a wrapper. ; When list1 comes in, it should be a ((1 2) (3) (3 4 5)) type list. (defun MY-SUB-UNION (set1 list1) (cond ((null list1) set1) ((null (car list1)) set1) (T (MY-SET-UNION set1 (MY-SUB-UNION (car list1) (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. ; Not that UNION takes more than two sets anyway. ;-) (defun MY-UNION (set1 &rest list1) (MY-SUB-UNION set1 list1) ) ; LISTIFY makes a two-level list out of whatever you pass it. (defun LISTIFY (&rest list1) (cons list1 nil) ) ; MY-SUB-COMPILE does the work for MY-COMPILE, which sets up the base case. ; A badly formed btree may cause this to return invalid "machine language". (defun MY-SUB-COMPILE (btree1 level1) (cond ((null btree1) nil) ((listp btree1) (cond ((eql (car btree1) '+) ; this is the ADD case (append (MY-SUB-COMPILE (cadr btree1) level1) (MY-SUB-COMPILE (caddr btree1) (+ level1 1)) (LISTIFY 'ADD level1 (+ level1 1)) ) ; append ) ; eql '+ cond line ((eql (car btree1) '-) ; this is the SUB case (append (MY-SUB-COMPILE (cadr btree1) level1) (MY-SUB-COMPILE (caddr btree1) (+ level1 1)) (LISTIFY 'SUB level1 (+ level1 1)) ) ; append ) ; eql '- cond line ((eql (car btree1) '*) ; this is the MUL case (append (MY-SUB-COMPILE (cadr btree1) level1) (MY-SUB-COMPILE (caddr btree1) (+ level1 1)) (LISTIFY 'MUL level1 (+ level1 1)) ) ; append ) ; eql '* cond line ((eql (car btree1) '/) ; this is the DIV case (append (MY-SUB-COMPILE (cadr btree1) level1) (MY-SUB-COMPILE (caddr btree1) (+ level1 1)) (LISTIFY 'DIV level1 (+ level1 1)) ) ; append ) ; eql '/ cond line ) ; 2nd level cond ) ; listp cond line (T (LISTIFY 'MOVE level1 btree1)) ; this is the MOVE case ) ; 1st level cond ) ; defun MY-SUB-COMPILE ; MY-COMPILE takes a btree and returns its "machine language" translation. (defun MY-COMPILE (btree1) (MY-SUB-COMPILE btree1 1) ; start processing on level 1 )