Extending the & & rest parameter in Common Lisp

Suppose I'm tired of writing "format t ..." all the time and want something a little less than keystrokes.

So, I am writing this:

(defun puts (fstring &rest vars) (format t fstring vars)) (puts "~a ~a" 1 2) ;; error message results, because vars became (1 2) 

Now vars been converted to a list of any parameters that I went through. It should be "expanded" from the list of values.

What is a typical solution to this problem?

+7
source share
2 answers

You can use apply for this: (apply #'format t fstring vars) extends vars into separate arguments to format .

+11
source

In addition to apply , there is also the opportunity to do this with a macro in which you can use ,@ to merge lists inside backquotes:

 (defmacro puts (fstring &rest vars) `(format t ,fstring ,@vars)) 
+2
source

All Articles