How to Work With Lists In Erlang?

8 minutes read

In Erlang, lists are fundamental data structures that can store elements of any type, including other lists. They are represented by square brackets ([ ]) and can be manipulated using several built-in functions. Here are some common operations you can perform when working with lists in Erlang:

  1. Creating Lists: Lists can be created by simply enclosing elements within square brackets. For example, [1, 2, 3] creates a list containing the integers 1, 2, and 3.
  2. Retrieving List Elements: The head of a list (first element) can be obtained using the hd/1 function. The tail of a list (list excluding the head) can be obtained using the tl/1 function.
  3. Appending and Prepending Elements: Elements can be appended to a list using the ++/2 operator. For example, [1, 2] ++ [3, 4] results in [1, 2, 3, 4]. Elements can be prepended to a list using the cons operator (|). For example, 1 | [2, 3, 4] results in [1, 2, 3, 4].
  4. Checking List Length: The length of a list can be determined using the length/1 function.
  5. Accessing List Elements by Index: Erlang lists are 0-indexed, meaning the first element is at index 0, the second at index 1, and so on. To access an element at a specific index, you can use recursion or pattern matching.
  6. List Comprehensions: List comprehensions provide a concise way to create new lists based on existing ones. They often involve filtering and transforming elements of the original list.
  7. List Manipulation: Erlang provides various built-in functions for manipulating lists, such as lists:reverse/1 to reverse a list, lists:nth/2 to get the element at a specific index, lists:map/2 to apply a function to each element, and lists:filter/2 to filter elements based on a condition.


It's worth noting that Erlang lists are implemented as linked lists, so appending or removing elements from the end of a long list can be inefficient. In such cases, it may be more efficient to work with other data structures like tuples or binary data.

Best Erlang Books to Read in 2024

1
Handbook of Neuroevolution Through Erlang

Rating is 5 out of 5

Handbook of Neuroevolution Through Erlang

2
Designing for Scalability with Erlang/OTP: Implement Robust, Fault-Tolerant Systems

Rating is 4.9 out of 5

Designing for Scalability with Erlang/OTP: Implement Robust, Fault-Tolerant Systems

3
Learn You Some Erlang for Great Good!: A Beginner's Guide

Rating is 4.8 out of 5

Learn You Some Erlang for Great Good!: A Beginner's Guide

4
Erlang Programming: A Concurrent Approach to Software Development

Rating is 4.7 out of 5

Erlang Programming: A Concurrent Approach to Software Development

5
Programming Erlang: Software for a Concurrent World

Rating is 4.6 out of 5

Programming Erlang: Software for a Concurrent World

6
Erlang and OTP in Action

Rating is 4.5 out of 5

Erlang and OTP in Action

7
Erlang and Elixir for Imperative Programmers

Rating is 4.4 out of 5

Erlang and Elixir for Imperative Programmers

8
Property-Based Testing with PropEr, Erlang, and Elixir: Find Bugs Before Your Users Do

Rating is 4.3 out of 5

Property-Based Testing with PropEr, Erlang, and Elixir: Find Bugs Before Your Users Do


How to split a list into two parts in Erlang?

Here is an example of how you can split a list into two parts in Erlang:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
split_list([]) ->
    {[], []};
split_list([X]) ->
    {[X], []};
split_list(List) ->
    {Part1, Part2} = split_list(List, 1, [], []),
    {lists:reverse(Part1), lists:reverse(Part2)}.

split_list([], _, Acc1, Acc2) ->
    {Acc1, Acc2};
split_list([X|Xs], Index, Acc1, Acc2) ->
    case Index rem 2 of
        1 -> split_list(Xs, Index+1, [X|Acc1], Acc2);
        0 -> split_list(Xs, Index+1, Acc1, [X|Acc2])
    end.


You can call the split_list/1 function with your input list as the argument. This function returns a tuple containing two lists, where the first half of the input list is stored in the first element of the tuple, and the second half is stored in the second element of the tuple.


Example usage:

1
2
3
List = [1, 2, 3, 4, 5, 6],
{Part1, Part2} = split_list(List),
io:format("Part 1: ~p~nPart 2: ~p~n", [Part1, Part2]).


Output:

1
2
Part 1: [1,2,3]
Part 2: [4,5,6]



How to create a list in Erlang?

In Erlang, lists are created using square brackets [] and elements are separated by commas. Here's an example of how you can create a list:

  1. Create an empty list:
1
EmptyList = [].


  1. Create a list with multiple elements:
1
List = [1, 2, 3, 4, 5].


This list contains the numbers 1, 2, 3, 4, and 5.

  1. Create a list with different types of elements:
1
MixedList = ["Erlang", 42, true, {key, value}].


This list contains a string "Erlang", the number 42, a boolean value true, and a tuple {key, value}.


You can also include variables as elements in a list:

1
2
Variable = 10,
ListWithVariable = ["Hello", "World", Variable].


In this case, the variable Variable will be evaluated and its value will be included in the list.


Note: Lists in Erlang are immutable, meaning that once a list is created, it cannot be modified. However, you can perform various operations on lists to obtain new lists with desired elements.


How to concatenate two lists in Erlang?

To concatenate two lists in Erlang, you can use the built-in concatenation operator ++. Here's an example of how to use it:

1
2
3
List1 = [1, 2, 3],
List2 = [4, 5, 6],
Concatenated = List1 ++ List2.


In this example, List1 is [1, 2, 3], List2 is [4, 5, 6], and Concatenated is [1, 2, 3, 4, 5, 6].

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

Erlang is a programming language that has gained popularity for developing scalable and fault-tolerant systems, including web applications. When it comes to web development, Erlang offers several frameworks and libraries that facilitate the process. Here is an...
To install Erlang on Windows, follow these steps:Visit the official Erlang website at www.erlang.org.Go to the "Download" section of the website.Choose the Windows option under the "OTP" (Open Telecom Platform) category.Select the latest versio...
To send and receive messages between Erlang processes, you can use the message-passing mechanism provided by the Erlang programming language. Here are the key points to understand:Process Identification: In Erlang, processes are identified by a unique process ...
Working with binary data in Erlang allows for efficient manipulation and processing of binary data structures. Erlang provides a set of built-in functions and syntax for working with binary data. Here are some important aspects of working with binary data in E...
In Erlang, you can join a list of integers into a string by using the lists:concat/1 function along with the lists:map/2 function to convert each integer to its corresponding string representation.Here is an example code snippet to demonstrate the process: joi...
To configure Docker to expose an Erlang node, you need to follow these steps:Create a Dockerfile: First, create a Dockerfile in your project directory. This file will specify the base image, dependencies, and configurations for your Docker container. Choose an...