This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Creating args from list

7 posts · 7524 views

I'm trying to work out a way of providing mutliple arguments to a function, where the arguments are held in a variable as a list. I've got a stripped down example of what I'm trying to do:
(defun print-record (format-str &rest args)
  (format t format-str args))

(print-record "~A ~A~%" 'first-arg 'second-arg)
The format string doesn't expect a list for its arguments, so this fails. I was hoping that 'multiple-value-call' might work, but it keeps list arguments as lists too :-( e.g.:
   (multiple-value-call #'format (values t format-str args))
(yes, I know that 'format' can iterate over a list to print multiple records, but that isn't what I want here).

Is there any way to do this with CL ?

Cheers,
Chris

Re: Creating args from list

You want to use apply.
(apply #'format t format-str args)
See if that does what you want.

Re: Creating args from list

Thanks - that worked a treat :-)

You can probably tell I'm new to CL.

Cheers,
Chris

Re: Creating args from list

Hey, that's what we're here for. :)

If you have questions about what #'format really means, feel free to ask.

Re: Creating args from list

That is exactly what I was looking for. I'm use to doing funcall. Woohoo this should work!

Thanks a bunch!