Page 1 of 1

Creating args from list

Posted: Mon Jul 21, 2008 5:57 pm
by chrisf
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:

Code: Select all

(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.:

Code: Select all

   (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

Posted: Mon Jul 21, 2008 6:21 pm
by Geoff Wozniak
You want to use apply.

Code: Select all

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

Re: Creating args from list

Posted: Mon Jul 21, 2008 6:27 pm
by chrisf
Thanks - that worked a treat :-)

You can probably tell I'm new to CL.

Cheers,
Chris

Re: Creating args from list

Posted: Mon Jul 21, 2008 6:30 pm
by Geoff Wozniak
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

Posted: Mon Dec 15, 2008 9:52 am
by gutzofter
That is exactly what I was looking for. I'm use to doing funcall. Woohoo this should work!

Thanks a bunch!

Re: Creating args from list

Posted: Thu Jan 15, 2009 7:40 pm
by Harleqin
This is interesting. Read up on a spreadable argument list designator.

Re: Creating args from list

Posted: Fri Jan 16, 2009 3:05 pm
by findinglisp
Harleqin wrote:This is interesting. Read up on a spreadable argument list designator.
I learn so much just from reading the CLHS glossary. :) It's amazing all the little tidbits lurking in there.