¿Eliminar todas las ocurrencias de un valor en una lista en Erlang?

I have a list of values that looks something like this:

["Some", "random", "values", [], "in", "a", [], "list"].

I would like to create a new list with the empty list items removed. Like this:

["Some", "random", "values", "in", "a", "list"].

What is the easiest way of going about this? I assume using list comprehensions to build a new list is going to be the most efficient way of doing this. How would I filter this list using list comprehensions?

preguntado el 28 de mayo de 14 a las 13:05

2 Respuestas

List comprehensions are a neater lists:filter/2:

[E || E <- List, E /= []]

contestado el 28 de mayo de 14 a las 14:05

Esto se puede lograr usando lists:filter.

List = ["Some", "random", "values", [], "in", "a", [], "list"],
lists:filter(fun(X) -> X /= [] end, List).

lists:filter takes a fun and a list. The fun should take a list item and return true or false. If the fun returns true the item is returned in the new list.

Más información puede ser encontrada aquí: http://erlangcentral.org/wiki/index.php?title=Filtering_Lists

contestado el 28 de mayo de 14 a las 13:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.