Editing
Elixir Lists
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Elixir Lists == The Elixir programming language provides a powerful data structure known as a list. In Elixir, a list is an ordered collection of elements. Lists can contain elements of any data type, including numbers, strings, atoms, and even other lists. Lists in Elixir are immutable, which means they cannot be modified once created. Instead, operations on lists return new lists. === Creating Lists === To create a list in Elixir, we can use square brackets `[ ]` and separate the elements with commas. ```elixir my_list = [1, 2, 3, 4, 5] ``` We can also use the `++` operator to concatenate two lists together. ```elixir list1 = [1, 2, 3] list2 = [4, 5, 6] combined_list = list1 ++ list2 ``` === Accessing List Elements === In Elixir, we can access individual elements in a list using the square bracket notation. ```elixir my_list = [1, 2, 3, 4, 5] second_element = my_list[1] ``` We can also use pattern matching to extract elements from a list. ```elixir [first_element | rest_of_list] = my_list ``` This will bind the variable `first_element` to the first element of the list and `rest_of_list` to the remaining elements. === List Operations === Elixir provides a wide range of functions to manipulate lists: - `List.length/1` - Returns the length of a list. - `List.first/1` - Returns the first element of a list. - `List.last/1` - Returns the last element of a list. - `List.flatten/1` - Flattens a nested list into a single level list. - `List.reverse/1` - Reverses the order of elements in a list. - `List.delete/2` - Deletes the first occurrence of an element in a list. - `List.duplicate/2` - Creates a new list with a given element duplicated a certain number of times. === List Comprehensions === Elixir supports list comprehensions, a powerful syntax for transforming and filtering lists. ```elixir squares = for x <- [1, 2, 3, 4, 5], do: x * x ``` This will produce a new list `squares` containing the squares of the elements in the original list. === Conclusion === Lists are a fundamental data structure in Elixir and understanding their operations and usage is crucial for writing efficient and effective Elixir code. By utilizing the operations and syntax described in this article, developers can manipulate and transform lists to suit their specific needs.
Summary:
Please note that all contributions to Elixir Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Elixir Wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information