Creating args from list

Discussion of Common Lisp

Creating args from list

Postby 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
chrisf
 
Posts: 2
Joined: Mon Jul 21, 2008 5:38 pm

Re: Creating args from list

Postby 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.
Geoff Wozniak
 
Posts: 9
Joined: Wed Jul 16, 2008 5:38 pm
Location: Canada

Re: Creating args from list

Postby chrisf » Mon Jul 21, 2008 6:27 pm

Thanks - that worked a treat :-)

You can probably tell I'm new to CL.

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

Re: Creating args from list

Postby 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.
Geoff Wozniak
 
Posts: 9
Joined: Wed Jul 16, 2008 5:38 pm
Location: Canada

Re: Creating args from list

Postby 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!
gutzofter
 
Posts: 10
Joined: Thu Jul 03, 2008 12:53 pm

Re: Creating args from list

Postby Harleqin » Thu Jan 15, 2009 7:40 pm

This is interesting. Read up on a spreadable argument list designator.
Harleqin
 
Posts: 71
Joined: Wed Dec 17, 2008 5:18 am
Location: Bonn, Germany

Re: Creating args from list

Postby 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/
findinglisp
 
Posts: 440
Joined: Sat Jun 28, 2008 7:49 am
Location: Austin, TX


Return to Common Lisp

Who is online

Users browsing this forum: Google [Bot] and 3 guests