Erlang – Lists

Erlang lists

In this guide, we will discuss Erlang lists. The List is a structure used to store a collection of data items. In Erlang, Lists are created by enclosing the values in square brackets.

Following is a simple example of creating a list of numbers in Erlang.

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

[1 2 3]

Let us now discuss the various methods available for Lists. Note that the lists library needs to be imported for these methods to work.

Sr.NoMethod and Description
1all
Returns true if Pred(Elem) returns true for all elements Elem in List, otherwise false.
2any
Returns true if Pred(Elem) returns true for at least one element Elem in List.
3append
Returns a new list List3 which is made from the elements of List1 followed by the elements of List2.
4delete
Deletes an element from the list and returns a new list.
5droplast
Drops the last element of a List.
6duplicate
Returns a list which contains N copies of the term Elem
7last
Returns the last element of the list
8max
Returns the element of the list which has the maximum value.
9member
Checks if an element is present in the list or not.
10min
Returns the element of the list which has the minimum value.
11merge
Returns the sorted list formed by merging all the sub-lists of ListOfLists.
12nth
Returns the Nth element of List.
13nthtail
Returns the Nth tail of the List.
14reverse
Reverses a list of elements.
15sort
Sorts a list of elements.
16sublist
Returns a sublist of elements.
17sum
Returns the sum of elements in the list.

Next Topic : Click Here

This Post Has One Comment

Leave a Reply