On Thu, Jul 12, 2007 at 06:58:04AM -0400, Larry Clapp wrote:
Your solution highlights some of the possible axes of judging, aka design trade-offs, kind of like Kent is always harping^W reminding us about in c.l.l. Specifically, your solution conses a little more than mine, but since you use reduce and mapcan and I use loop (twice), yours seems "Lispier".
My solution:
(defun center (&rest list) (let* ((max-length (loop for item in list maximize (length item))) (center (floor max-length 2))) (loop for item in list for length = (length item) collect (format nil "~v,0T~A" (- center (floor length 2)) item))))
Outputs:
QUIZ 15 > (center "this" "is" "a test of the" "center function") (" this" " is" " a test of the" "center function")
I realize now that I should simplify my math:
(defun center2 (&rest list) (let ((max-length (loop for item in list maximize (length item)))) (loop for item in list collect (format nil "~v,0T~A" (floor (- max-length (length item)) 2) item))))
So far, I like Sean's solution the best. For reference, Sean wrote:
(defun center (&rest list) (let ((max (reduce #'max vals :key #'length))) (mapcar #'(lambda (x) (format nil "~,,v@A" (floor (- max (length x)) 2) x)) list)))
-- L