Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to extract all elements of a list in place

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.

like image 453
snowape Avatar asked Nov 23 '11 14:11

snowape


People also ask

How do you pop all elements in a list?

The methods are remove(), pop() and clear(). It helps to remove the very first given element matching from the list. The pop() method removes an element from the list based on the index given. The clear() method will remove all the elements present in the list.

How do you find all the elements in the list is same in Python?

You can convert the list to a set. A set cannot have duplicates. So if all the elements in the original list are identical, the set will have just one element. if len(set(input_list)) == 1: # input_list has all identical elements.


3 Answers

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.

like image 124
Svante Avatar answered Oct 06 '22 18:10

Svante


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.

like image 41
Wyetro Avatar answered Oct 06 '22 18:10

Wyetro


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.)

like image 25
Chris Jester-Young Avatar answered Oct 06 '22 18:10

Chris Jester-Young