Functional Programming Lists - Functional Programming

What are functional programming lists?

List is called as the latest data type in functional programming languages used to store a collection of similar data items. This is most similar concept to arrays in object-oriented programming. Here List items can be written in a square bracket separated by commas. The way of highlighting this is differ from language to language.

Program to Create a List of Numbers in Java

List is not called as a data type in Java/C/C++, but here we have different ways to create a list in Java, i.e., by using ArrayList and LinkedList.
Below mentioned example describes how to create a list in Java. Here we are using a Linked List method to create a list of numbers.
It will produce the following output –
Program to Create a List of Numbers in Erlang
It will produce the following output –

List Operations in Java

This part will discuss some operations that can be done over lists in Java.

Adding Elements into a List

Various methods like add(Object), add(index, Object), addAll() are used to add elements into a list. For example,

Removing Elements from a List

You can use elements from a list by using remove(index) or removeobject() from a list. For example,
Note − To remove all elements from the list clear() method is used.

Retrieving Elements from a List

Use the get() method to retrieve elements from a list at a specified location. The getfirst() & getlast() methods can be used in LinkedList class. For example,

Updating Elements in a List

Use the set(index,element) method to update an element at a specified index with a specified element. For Example,

Sorting Elements in a List

Use the collection.sort() and collection.reverse() methods to sort a list in ascending or descending order. For example,

Searching Elements in a List

Use following three methods as per the requirement −
  • Boolean contains(Object) method returns true if the list contains the specified element, else it returns false.
  • int indexOf(Object) method returns the index of the first occurrence of a specified element in a list, else it returns -1 when the element is not found.
  • int lastIndexOf(Object) returns the index of the last occurrence of a specified element in a list, else it returns -1 when the element is not found.

List Operations in Erlang

This section describes about some operations that can be done over lists in Erlang.

Adding two lists

Use the append(listfirst, listsecond) method to create a new list by adding two lists. For example,

Deleting an element

Use the delete(element, listname) method to delete the specified element from the list & it returns the new list. For example,

Deleting last element from the list

Use the droplast(listname) method to delete the last element from a list and return a new list. For example,

Searching an element

Use the member(element, listname) method to search the element into the list, if found it returns true else it returns false. For Example,

Getting maximum and minimum value

Use the max(listname) and min(listname) methods to find the maximum and minimum values in a list. For example,

Sorting list elements

Use sort(listname) and reverse(listname) methods to sort a list in ascending or descending order. For example,

Adding list elements

Use sum(listname) method to add all the elements of a list and return their sum. For example,

Sort a list in ascending and descending order using Java

Below mentioned program display how to sort a list in ascending and descending order using Java –
It will produce the following output –

Sort a list in ascending order using Erlang

Below mentioned program display how to sort a list in ascending and descending order by using Erlang, which is a functional programming language –
It will produce the following output –

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Functional Programming Topics