You forgot to pass the first argument (
result-type
) toconcatenate
.You forgot to pass the first argument (
destination
) toformat
.Your default argument handling can be simplified.
Please avoid dangling parens, they violate the accepted style.
To answer your actual question: use
loop
Here is my code:
(defun dump-list (list-to-dump &optional (prefix ""))
(unless (stringp prefix) ; or (check-type prefix string) instead of the whole unless form
(setq prefix (princ-to-string prefix))) ; or (coerce prefix 'string) instead of princ
(fresh-line) ; start with a new line if something has already been printed
(loop :for (label item) :on list-to-dump :by #'cddr :do
(format t "~a~a:" prefix label)
(if (consp item) ; treat NIL as a single value, not a list
(dump-list item (concatenate 'string prefix " "))
(format t " ~a~%" item))))
It prints what you want:
LABEL1: VALUE1
LABEL2: VALUE2
LABEL3:
NESTEDLABEL1: NESTEDVALUE1
NESTEDLABEL2: NESTEDVALUE2
NESTEDVALUE3:
TOOMANYLAYERS: YEP.
SORRY: ABOUT THAT.
If you insist on checking the type of prefix
(instead of converting it to a string), please use check-type
as indicated in the code.