English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Erlang Lists

A list is a structure used to store a collection of data items. In Erlang, lists are created by enclosing values in square brackets.

The following is a simple example of creating a number list in Erlang.

Online example

-module(helloworld). 
-export([start/0]). 
start() -> 
   Lst1 = [1,2,3], 
   io:fwrite("~w~n",[Lst1]).

The output of the above example will be-

Output results

[1 2 3]

Now let's discuss the various methods available for lists. Please note that the list library needs to be imported for these methods to take effect.

Serial numberMethods and descriptions
1

all

If Pred (Elem) returns true for all elements Elem in the List, then return true; otherwise, return false.

2

any

Returns true if Pred (Elem) returns true for at least one element Elem in the List.

3

append

Return a new list List3The list is made up of List1of the elements and List2consists of the elements.

4

delete

Delete an element from the list and return a new list.

5

droplast

Delete the last element of the list.

6

duplicate

Return a list containing N copies of the term Elem

7

last

Return the last element of the list

8

max

Return the element with the maximum value in the list.

9

member

Check if an element exists in the list.

10

min

Return the element with the minimum value in the list.

11

merge

Return a sorted list formed by merging all sublists of ListOfLists.

12

nth

Return the Nth element of the List.

13

nthtail

Return the Nth tail of the list.

14

reverse

Reverse the element list.

15

sort

Sort the element list.

16

sublist

Return the sublist of elements.

17

sum

Return the sum of the elements in the list.