(defun pprint-array (stream array &optional colon amp (delimiter #\Space)) (declare (ignore colon amp)) (loop :with first-time = t :for x :across array :unless first-time :do (format stream "~C" delimiter) :end :do (format stream "~S" x) (setf first-time nil))) (format t "~' :@/pprint-array/" #(1 2 3 4)) ; 1 2 3 4
You can add more arguments (they will be separated by a comma), or you can also handle the colon and ampersand somehow.
Following Svante, we advise here a slightly modified version of this function, it also uses a colon and ampersand as follows: the colon makes it change between prin1 and princ , and at-sign allows you to print nested arrays recursively (it can be even more complicated to also print multidimensional arrays, etc .... but with a limited time here, what is it:
(defun pprint-array (stream array &optional colon amp (delimiter #\Space) (line #\Newline)) (if amp (loop :with first-time = t :for a :across array :unless first-time :do (when line (write-char line stream)) :end :if (or (typep a 'array) (typep a 'vector)) :do (pprint-array stream a colon amp delimiter line) :else :do (if colon (prin1 a stream) (princ a stream)) :end :do (setf first-time nil)) (loop :with first-time = t :for x :across array :unless first-time :do (when delimiter (write-char delimiter stream)) :end :do (if colon (prin1 x stream) (princ x stream)) (setf first-time nil))))
user797257
source share