0

After some trial & error, I've managed to code a macro mashup to print the names & values of several variables (for debugging). It seems to work, but I'd like to see how to code this more "professionally"?

(defmacro prt1 (var) ;Print a single variable and its value. `(progn (princ (symbol-name ',var)) (princ " = ") (princ ,var) (terpri))) (defmacro prt (&rest vars) ;Print the values of a number of variable names. (eval `(append (list 'progn) (map 'list #'(lambda (x) (list 'prt1 x)) ',vars) (list (list 'terpri)) (list t)))) ;need to return t 

Calling (prt A B C) then prints the current bindings--eg:

A = 1 B = 2 C = 3 T 

1 Answer 1

5
(defmacro prt1 (var) "Print a single variable and its value." `(format t "~a = ~a~%" ',var ,var)) (defmacro prt (&rest vars) "Print the values of variables." `(progn ,@(loop for var in vars collect `(prt1 ,var)))) 
Sign up to request clarification or add additional context in comments.

1 Comment

Nice Rainer--but tried some minor modifications that just won't work!? (defmacro prt (&rest vars) "Print the values of variables." (progn (format t "~%") ,@(loop for var in vars collect (prt1 ,var)) (finish-output t) t)) Extra ~% to space output is ignored, and finish-output doesn't. The only change that does work is returning t! (I'm on SBCL.)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.