Answer Key For Exam #1 Review
;;; Question #1
(defstruct triangle a b c)
(defun eq-tri (x)
(and (listp x)
(mapcar #'(lambda (s)
(make-triangle :a s :b s :c s)
)
x
)
)
)
;;; Question #2
(defun stars (y)
(progn
(format t "~%")
(dotimes (yi y 'Done)
(dotimes (x (+ yi 1) (format t "~%"))
(format t "*")
)
)
)
)
;; -- or --
(defun stars2 (y)
(progn (format t "~%")
(do ((i 0 (+ i 1)))
((= i y) 'Done)
(do ((x 0 (+ x 1)))
((= x (+ i 1)) (format t "~%"))
(format t "*")
)
)
)
)
;;; Question #3
(defun array-adder (old-array len &optional (addit 0))
(do ((new-arr (make-array len))
(count 0 (+ count 1))
)
((= count len) new-arr)
(setf (aref new-arr count) (+ (aref old-array count) addit))
)
)
;;; Question #4
(defun make-rectangle (&key (length 0) (width 0))
(cons length width)
)
(defun rectangle-length (rect)
(car rect)
)
(defun rectangle-width (rect)
(cdr rect)
)
;;; Question #5a
(defstruct my-circle radius (area (* pi radius radius)) (circum (* 2 pi radius)))
;; #5b
(defun silly (rad1 rad2)
(cond
((> rad1 rad2) (my-circle-area (make-my-circle :radius rad1)))
((> rad2 rad1) (my-circle-circum (make-my-circle :radius rad2)))
(t 'EQUAL)
)
)
;; #5c
(defun big-area (&rest radii)
(my-circle-area (make-my-circle :radius (car (sort radii #'>))))
)
;; #5d
(defun cylinder (circ height)
(and (my-circle-p circ)
(let ((vol (* (my-circle-area circ) height))
(sa (* (my-circle-circum circ) height))
)
(format t "~%The area is: ~A. The volume is: ~A. " sa vol)
(if (> vol sa)
;then
(format t "The volume is bigger.~%")
;else
(format t "The surface area is bigger.~%")
)
)
)
)
;; #5e
(let ((biggest-area nil))
(defun biggest (circ)
(cond
((not (my-circle-p circ)) biggest-area)
((null biggest-area) (setf biggest-area (my-circle-area circ)))
((> (my-circle-area circ) biggest-area) (setf biggest-area (my-circle-area circ)))
(t biggest-area)
)
)
)
Back to Main Page...
Kenneth W. Flynn
flynnk@rpi.edu