Creating args from list

Discussion of Common Lisp
Post Reply
chrisf
Posts: 2
Joined: Mon Jul 21, 2008 5:38 pm

Creating args from list

Post by chrisf » Mon Jul 21, 2008 5:57 pm

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

Geoff Wozniak
Posts: 9
Joined: Wed Jul 16, 2008 5:38 pm
Location: Canada

Re: Creating args from list

Post by Geoff Wozniak » Mon Jul 21, 2008 6:21 pm

You want to use apply.

Code: Select all

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

chrisf
Posts: 2
Joined: Mon Jul 21, 2008 5:38 pm

Re: Creating args from list

Post by chrisf » Mon Jul 21, 2008 6:27 pm

Thanks - that worked a treat :-)

You can probably tell I'm new to CL.

Cheers,
Chris

Geoff Wozniak
Posts: 9
Joined: Wed Jul 16, 2008 5:38 pm
Location: Canada

Re: Creating args from list

Post by Geoff Wozniak » Mon Jul 21, 2008 6:30 pm

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

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

gutzofter
Posts: 10
Joined: Thu Jul 03, 2008 12:53 pm

Re: Creating args from list

Post by gutzofter » Mon Dec 15, 2008 9:52 am

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

Thanks a bunch!

Harleqin
Posts: 71
Joined: Wed Dec 17, 2008 5:18 am
Location: Bonn, Germany

Re: Creating args from list

Post by Harleqin » Thu Jan 15, 2009 7:40 pm

This is interesting. Read up on a spreadable argument list designator.

findinglisp
Posts: 447
Joined: Sat Jun 28, 2008 7:49 am
Location: Austin, TX
Contact:

Re: Creating args from list

Post by findinglisp » Fri Jan 16, 2009 3:05 pm

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.
Cheers, Dave
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/

Post Reply