¿Eliminar todas las ocurrencias de un valor en una lista en Erlang?
Frecuentes
Visto 1,814 equipos
1
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?
2 Respuestas
9
List comprehensions are a neater lists:filter/2:
[E || E <- List, E /= []]
contestado el 28 de mayo de 14 a las 14:05
4
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 list erlang filtering list-comprehension or haz tu propia pregunta.