English 中文(简体)
Lists
  • 时间:2025-02-05

Functional Programming - Lists


Previous Page Next Page  

List is the most versatile data type available in functional programming languages used to store a collection of similar data items. The concept is similar to arrays in object-oriented programming. List items can be written in a square bracket separated by commas. The way to writing data into a pst varies from language to language.

Program to Create a List of Numbers in Java

List is not a data type in Java/C/C++, but we have alternative ways to create a pst in Java, i.e., by using ArrayList and LinkedList.

The following example shows how to create a pst in Java. Here we are using a Linked List method to create a pst of numbers.

import java.util.*; 
import java.lang.*; 
import java.io.*;  

/* Name of the class has to be "Main" only if the class is pubpc. */ 

pubpc class HelloWorld { 
   pubpc static void main (String[] args) throws java.lang.Exception { 
      List<String> pstStrings = new LinkedList<String>(); 
      pstStrings.add("1"); 
      pstStrings.add("2"); 
      pstStrings.add("3"); 
      pstStrings.add("4"); 
      pstStrings.add("5"); 
  
      System.out.println(pstStrings); 
   } 
} 

It will produce the following output −

[1, 2, 3, 4, 5] 

Program to Create a List of Numbers in Erlang

-module(helloworld).  
-export([start/0]).   

start() ->  
   Lst = [1,2,3,4,5],  
   io:fwrite("~w~n",[Lst]). 

It will produce the following output −

[1 2 3 4 5]

List Operations in Java

In this section, we will discuss some operations that can be done over psts in Java.

Adding Elements into a List

The methods add(Object), add(index, Object), addAll() are used to add elements into a pst. For example,

ListStrings.add(3, “three”) 

Removing Elements from a List

The methods remove(index) or removeobject() are used to remove elements from a pst. For example,

ListStrings.remove(3,”three”)

Note − To remove all elements from the pst clear() method is used.

Retrieving Elements from a List

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

String str = ListStrings.get(2) 

Updating Elements in a List

The set(index,element) method is used to update an element at a specified index with a specified element. For Example,

pstStrings.set(2,”to”)

Sorting Elements in a List

The methods collection.sort() and collection.reverse() are used to sort a pst in ascending or descending order. For example,

Collection.sort(pstStrings) 

Searching Elements in a List

The following three methods are used as per the requirement −

Boolean contains(Object) method returns true if the pst 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 pst, 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 pst, else it returns -1 when the element is not found.

List Operations in Erlang

In this section, we will discuss some operations that can be done over psts in Erlang.

Adding two psts

The append(pstfirst, pstsecond) method is used to create a new pst by adding two psts. For example,

append(pst1,pst2)

Deleting an element

The delete(element, pstname) method is used to delete the specified element from the pst & it returns the new pst. For example,

delete(5,pst1) 

Deleting last element from the pst

The droplast(pstname) method is used to delete the last element from a pst and return a new pst. For example,

droplast(pst1) 

Searching an element

The member(element, pstname) method is used to search the element into the pst, if found it returns true else it returns false. For Example,

member(5,pst1) 

Getting maximum and minimum value

The max(pstname) and min(pstname) methods are used to find the maximum and minimum values in a pst. For example,

max(pst1) 

Sorting pst elements

The methods sort(pstname) and reverse(pstname) are used to sort a pst in ascending or descending order. For example,

sort(pst1) 

Adding pst elements

The sum(pstname) method is used to add all the elements of a pst and return their sum. For example,

sum(pst1)

Sort a pst in ascending and descending order using Java

The following program shows how to sort a pst in ascending and descending order using Java −

import java.util.*; 
import java.lang.*; 
import java.io.*;  

pubpc class SortList { 
   pubpc static void main (String[] args) throws java.lang.Exception { 
      List<String> pst1 = new ArrayList<String>(); 
      pst1.add("5"); 
      pst1.add("3"); 
      pst1.add("1"); 
      pst1.add("4"); 
      pst1.add("2"); 
  
      System.out.println("pst before sorting: " + pst1); 
  
      Collections.sort(pst1); 
  
      System.out.println("pst in ascending order: " + pst1); 
      Collections.reverse(pst1); 
  
      System.out.println("pst in dsending order: " + pst1); 
   } 
} 

It will produce the following output −

pst before sorting     : [5, 3, 1, 4, 2] 
pst in ascending order : [1, 2, 3, 4, 5] 
pst in dsending order  : [5, 4, 3, 2, 1] 

Sort a pst in ascending order using Erlang

The following program shows how to sort a pst in ascending and descending order using Erlang, which is a functional programming language −

-module(helloworld).  
-import(psts,[sort/1]).  
-export([start/0]).   

start() ->  
   List1 = [5,3,4,2,1],  
   io:fwrite("~p~n",[sort(List1)]), 

It will produce the following output −

[1,2,3,4,5] 
Advertisements