问题
Im looking for a way to extract all the elements of a list in common lisp. Like this
[194]> (break-out-of-list '(a b c d))
A
B
C
D
Edit: The usage example I gave was not thought out very well, however I'm still curious if it is possible to break out of a list like in the example above.
回答1:
What you demonstrate seems to be the question how to get the elements of a list as multiple values:
CL-USER> (values 1 2 3)
1
2
3
CL-USER> (apply #'values '(1 2 3))
1
2
3
See also multiple-value-bind
and nth-value
in the hyperspec.
回答2:
Sure, just use apply
:
(defun wraptest (&rest arguments)
(apply #'test arguments))
This technically doesn't "break out of list"; it simply uses a list's elements as arguments to a function call.
(Disclaimer: I'm a Schemer, not a Common Lisper, and there may be a more-idiomatic way to achieve the same result in CL.)
回答3:
While (apply #'values '(1 2 3))
works there is also a function to this called values-list
which is used like this:
(values-list '(1 2 3))
And it has the same result.
回答4:
I think you might be looking for this:
http://cl-cookbook.sourceforge.net/macros.html#LtohTOCentry-2
That's mostly all there is to backquote. There are just two extra items to point out. First, if you write ",@e" instead of ",e" then the value of e is spliced into the result. So if v=(oh boy), then `(zap ,@v ,v) evaluates to (zap oh boy (oh boy)). The second occurrence of v is replaced by its value. The first is replaced by the elements of its value. If v had had value (), it would have disappeared entirely: the value of (zap ,@v ,v) would have been (zap ()), which is the same as (zap nil).
Reading your comments:
(some-macro (break-list '(a b c d))) is equivalent to (some-macro 'a 'b 'c 'd)
With this, you could do:
`(some-macro ,@'(a b c d))
and you'd get:
(some-macro a b c d)
来源:https://stackoverflow.com/questions/8243813/is-there-a-way-to-extract-all-elements-of-a-list-in-place