How to Work With Lists And Tuples In Erlang?

9 minutes read

Working with lists and tuples in Erlang is a core aspect of the language. Lists and tuples are both fundamental data types in Erlang and have their own unique characteristics and uses.


Lists:

  • Lists in Erlang are represented by a sequence of elements enclosed in square brackets.
  • Elements within a list can be of any type, including atoms, numbers, other lists, or tuples.
  • Lists can be created using the "[]" operator or by construction using various Erlang functions like "cons" and "append".
  • Lists are typically used to store collections of elements that may be dynamically sized or modified.
  • Lists in Erlang are immutable, meaning once created, they cannot be modified. Any operation on a list creates a new list.
  • Common operations on lists include appending, prepending, concatenating, splitting, reversing, and accessing elements by index.
  • Erlang provides a rich set of list manipulation functions and pattern matching capabilities that make working with lists efficient and concise.


Tuples:

  • Tuples in Erlang are represented by a sequence of elements enclosed in curly braces, separated by commas.
  • Elements within a tuple can be of any type and do not have to be of the same type, unlike lists.
  • Tuples are typically used to store fixed-size collections of related elements.
  • Tuples in Erlang are also immutable and cannot be modified once created.
  • Like lists, tuple operations always create new tuples instead of modifying existing ones.
  • Accessing tuple elements is done using pattern matching or the element() function that accepts the tuple and the desired index.
  • Tuples are often used to return multiple values from functions or group related data together.


In Erlang, understanding how to work with lists and tuples is crucial for writing efficient and idiomatic code. They provide powerful and flexible data structures that allow for a wide range of data manipulation operations.

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
Introducing Erlang: Getting Started in Functional Programming

Rating is 4.6 out of 5

Introducing Erlang: Getting Started in Functional Programming

6
Erlang and OTP in Action

Rating is 4.5 out of 5

Erlang and OTP in Action

7
Programming Erlang: Software for a Concurrent World

Rating is 4.4 out of 5

Programming Erlang: Software for a Concurrent World

8
Programming Erlang: Software for a Concurrent World (Pragmatic Programmers)

Rating is 4.3 out of 5

Programming Erlang: Software for a Concurrent World (Pragmatic Programmers)


How to check if an element is present in a list in Erlang?

To check if an element is present in a list in Erlang, you can use the lists:member/2 function from the lists module.


Here is an example:

1
2
3
4
5
6
7
8
-module(list_check).
-export([check_if_present/2]).

check_if_present(Element, List) ->
    case lists:member(Element, List) of
        true -> io:format("Element ~p is present in the list.~n", [Element]);
        false -> io:format("Element ~p is not present in the list.~n", [Element])
    end.


In this code, the check_if_present/2 function takes an Element and a List as input. It uses the lists:member/2 function to check if the Element is present in the List. If it is, it prints a message saying that the element is present. If it is not, it prints a message saying that the element is not present.


Here is an example usage:

1
2
3
4
5
6
7
8
1> c(list_check).
2> list_check:check_if_present(3, [1, 2, 3, 4, 5]).
Element 3 is present in the list.
ok

3> list_check:check_if_present(6, [1, 2, 3, 4, 5]).
Element 6 is not present in the list.
ok


In the above example, we call the check_if_present/2 function with different elements and lists to check if the elements are present in the lists.


How to access the length of a tuple in Erlang?

To access the length of a tuple in Erlang, you can use the size/1 or tuple_size/1 BIF (Built-In Function). Here's how:

1
2
3
4
5
6
7
Tuple = {1, 2, 3, 4, 5},
Length = size(Tuple).

% or

Tuple = {1, 2, 3, 4, 5},
Length = tuple_size(Tuple).


In both cases, the variable Length will contain the length of the tuple, which is 5 in this example. Note that tuples are 1-based in Erlang, so the first element starts at index 1.


How to check if a list is empty in Erlang?

In Erlang, you can check if a list is empty using the length/1 function or pattern matching.

  1. Using length/1 function:
1
2
isEmpty(List) ->
    length(List) =:= 0.


  1. Using pattern matching:
1
2
3
4
isEmpty([]) ->
    true;
isEmpty(_List) ->
    false.


Both approaches will return true if the list is empty ([]), and false otherwise.


How to check if an element is present in a tuple in Erlang?

In Erlang, you can use the lists:member/2 function to check if an element is present in a tuple. Here is an example:

1
2
3
4
Tuple = {1, 2, 3, 4, 5},
Element = 3,
Result = lists:member(Element, element_list(Tuple)),
io:format("Element ~p is present in the tuple: ~p~n", [Element, Result]).


In this example, we have a tuple Tuple containing values {1, 2, 3, 4, 5}. We want to check if the element 3 is present in this tuple. We use the lists:member/2 function to check for membership. The function returns true if the element is present and false otherwise. The result is then printed using io:format.


Note that element_list/1 is a helper function that converts the tuple to a list, as lists:member/2 requires a list as the second argument. Here's the implementation of element_list/1:

1
2
3
4
element_list({}) ->
    [];
element_list(Tuple) ->
    [element(1, Tuple) | element_list(element(2, Tuple))].


This function recursively extracts each element of the tuple using the element/2 function and appends them to the resulting list until the tuple is empty.


How to get the last element of a list in Erlang?

To get the last element of a list in Erlang, you can use pattern matching and recursion. Here's an example function that extracts the last element:

1
2
3
4
5
6
last_element([]) -> 
    null;
last_element([X]) -> 
    X;
last_element([_|Rest]) -> 
    last_element(Rest).


In the first clause, an empty list returns the atom null or any other value you want to signify that the list is empty. In the second clause, a list with only one element returns that element. In the third clause, the function recursively calls itself with the rest of the list until it reaches the last element. Note that this implementation assumes the input list is not empty.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

In Erlang, you can multiply lists of lists together by using list comprehensions or recursive functions. Here's an explanation of the process:Using list comprehensions: Define two lists of lists that you want to multiply, for example, ListA and ListB. Iter...
To create Erlang views in CouchDB, you need to perform the following steps:Install Erlang: Erlang is a programming language that is commonly used to create CouchDB views. You need to install the Erlang runtime environment on your system before you can create E...
To install Erlang Observer on a Mac, you can follow these steps:The first step is to download and install Erlang. You can visit the official Erlang website (https://www.erlang.org/downloads) and download the latest stable version for Mac. Once Erlang is instal...