;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;Author: Joey Baranski ;; ;;Description: ECE478 Homework #1 Solutions ;; ;;Modified: 2004-01-08 ;; ;;Some code based on ECE478 Lectures ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;q. 1 http://www.ece.pdx.edu/~baranskj/robotics ;;q. 2 test: car, cdr, caddr, cons, list, set, setq, prog, progn, cond, ;; atom, minusp, numberp, go, nconc, ;; reverse, nreverse, print, trace ;;**NOTE: The following functions are not available in the version of ;; ANSI Common Lisp that I am using: ;; greaterp, plus, minus, traceset, debug, number ;;**See results in attached output file** ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; function: trans ;;written by: Joey Baranski (based on ece478 examples) ;; modified: 2004-01-10 ;; pre: nil ;; post: tests for lisp functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;: (defun trans() (setq a '(a 2 (3 z q))) ;; equivalent to (set 'a '(blah)) (set 'c 7654) ;set an atom, note that setq symbol == set 'symbol (set 'b '(x q (r (s)) t)) ;; equivalent to (setq b '(blah)) (list '(1 2 3) '(a b 5) '(22 (44 bb) xx)) (atom a) ;returns nil (atom c) ;returns true (atom (car a)) ;returns true (atom (cdr a)) ;returns nil (set 'r 'abcdefg) ;sets symbol a = symbol abcdefg (setq s 'gfedcba) ;equivalent to set 'symbol (cons 'l 'm) ;lists l & m in dotted notation (cons a (cons '(x y z) '(q 38 657))) ;lists a and the rest of the stuff in dotted notation (evenp 3) ;returns nil, not an even number (evenp 4) ;T even number (oddp 3) ;returns t, odd # (oddp 4) ;returns nil, even # (setq tl '(dog cat chicken rabbit turtle frog)) (car tl) ;returns dog (cdr tl) ;returns chicken through crow (first tl) ;same as car (rest tl) ;same as cdr (cadr tl) ;car of the cdr of tl so, cat (caddr tl) ;car of the cdr of the cdr of tl, so chicken (nth 3 tl) ;return nth element of a zero based list so rabbit (nth 0 tl) ;return dog (nth 100 tl) ;return nil (progn '(do not return me) '(please return me)) ;returns last thing, so returns '(please return me) (nconc '(a b c) '(1 2 3)) ;returns (a b c 1 2 3) ; (tagbody ; (setq a1 10) ; (setq b1 12) ; here ; (print '(here)) ; there ; (print 'there)) ; (if (> a1 b1) ; (go there) ; (go here) ; ) ; ) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; function: alertifminus ;;written by: Joey Baranski (based on ece478 examples) ;; modified: 2004-01-10 ;; pre: list or atom x ;; post: returns a some message if negative number, some other ;; message if positive number, and some other message is not ;; an atom or number ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defun alertifminus(x) (if (and (atom x) (numberp x)) ;redundant to check number and atom (just wanted to use and) (if (minusp x) (format t "~A is a negative number" x) (format t "~A is a positive number or zero" x) ) (print "The parameter you supplied is not a number") ) ) ;;q. 3 reverse the order of atoms in a list ;; **See results in attached output file** (setq a '(First 2nd Third Fourth 5th)) (reverse a) ;prints reversed list to screen preserves order of list a (setq nl (reverse a)) ;sets nl to reverse of list a preserves order of list a (nreverse a) ;reverses order of a, so atoms in list a is now in reverse ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; function: rv ;;written by: Joey Baranski (based on ece478 examples) ;; modified: 2004-01-10 ;; pre: arbitrary data structure s ;; post: a reversed copy of arbitrary data structure s ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defun rv(s) (cond ( (atom s) s ) (T ( list (rv (cdr s)) (car s) ) )) ) ;;q. 4 copy an arbitrary list data structure on all levels except atoms ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; function: copystruct ;;written by: Joey Baranski (based on ece478 examples) ;; modified: 2004-01-08 ;; pre: arbitrary data structure s ;; post: a copy of arbitrary data structure s ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defun copystruct(s) (cond ( (atom s) s ) (T (cons (copystruct (car s)) (copystruct (cdr s))))) )