Is there any way to extract all list items in place

Im looking for a way to extract all the elements of a generic lisp list. Like this

[194]> (break-out-of-list '(abcd)) A B C D 

Edit: The use case I gave was not worked out very well, however Iโ€™m still wondering if itโ€™s possible to exit the list, as in the example above.

+4
source share
4 answers

It seems that you are demonstrating the question of how to get list items 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 hyperspecificity.

+3
source

Of course, just use apply :

 (defun wraptest (&rest arguments) (apply #'test arguments)) 

It is technically not โ€œoff the listโ€; it just uses list items as arguments to the function call.

(Disclaimer: I'm a Schemer, not a regular Lisper, and there may be a more idiomatic way to achieve the same result in CL.)

+2
source

I think you could find this:

http://cl-cookbook.sourceforge.net/macros.html#LtohTOCentry-2

This is basically all you need for backquote. There are only two additional points to indicate. 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 (zap oh boy (oh boy)). The second occurrence of v is replaced by its value. The first is replaced by elements of its value. If the value of v had the value (), it would completely disappear: the value of (zap, @v, v) would be (zap ()), which is the same as (zap nil).

Reading your comments:

(some-macro (break-list '(abcd))) is equivalent to (some-macro' a 'b' c 'd)

With this you can do:

 `(some-macro ,@'(abcd)) 

and you will receive:

 (some-macro abcd) 
+1
source

While (apply #'values '(1 2 3)) works, there is a function for this called values-list , which is used as follows:

 (values-list '(1 2 3)) 

And he has the same result.

+1
source

Source: https://habr.com/ru/post/1382715/


All Articles