English 中文(简体)
Haskell - More On Functions
  • 时间:2024-10-18

Haskell - More On Functions


Previous Page Next Page  

Till now, we have discussed many types of Haskell functions and used different ways to call those functions. In this chapter, we will learn about some basic functions that can be easily used in Haskell without importing any special Type class. Most of these functions are a part of other higher order functions.

Head Function

Head function works on a List. It returns the first of the input argument which is basically a pst. In the following example, we are passing a pst with 10 values and we are generating the first element of that pst using the head function.

main = do 
   let x = [1..10]   
   putStrLn "Our pst is:"  
   print (x) 
   putStrLn "The first element of the pst is:" 
   print (head x)

It will produce the following output −

Our pst is: 
[1,2,3,4,5,6,7,8,9,10]
The first element of the pst is:
1

Tail Function

Tail is the function that complements the head function. It takes a pst as the input and yields the entire pst without the head part. That means, the tail function returns the entire pst without the first element. Take a look at the following example −

main = do 
   let x = [1..10]   
   putStrLn "Our pst is:"  
   print (x) 
   putStrLn "The tail of our pst is:" 
   print (tail x) 

It will produce the following output −

Our pst is:
[1,2,3,4,5,6,7,8,9,10]
The tail of our pst is:
[2,3,4,5,6,7,8,9,10]

Last Function

As the name suggests, it yields the last element of the pst that is provided as the input. Check the following example.

main = do 
   let x = [1..10]   
   putStrLn "Our pst is:"  
   print (x) 
   putStrLn "The last element of our pst is:" 
   print (last x)

It will produce the following output −

Our pst is:
[1,2,3,4,5,6,7,8,9,10]
The last element of our pst is:
10

Init Function

Init works exactly as the opposite of tail function. It takes a pst as an argument and returns the entire pst without the last entry.

main = do 
   let x = [1..10]   
   putStrLn "Our pst is:"  
   print (x) 
   putStrLn "Our pst without the last entry:"  
   print (init x) 

Now, observe its output −

Our pst is:
[1,2,3,4,5,6,7,8,9,10]
Our pst without the last entry:
[1,2,3,4,5,6,7,8,9]

Null Function

Null is a Boolean check function which works on a String and returns True only when the given pst is empty, otherwise it returns False. The following code checks whether the suppped pst is empty or not.

main = do 
   let x = [1..10]   
   putStrLn "Our pst is:"  
   print (x) 
   putStrLn "Is our pst empty?"  
   print (null x)

It will produce the following output −

Our pst is:
[1,2,3,4,5,6,7,8,9,10]
Is our pst empty?
False

Reverse Function

It works on a String input and converts the entire input into reverse order and give one output as a result. Below is the code base for this function.

main = do 
   let x = [1..10]  
   putStrLn "Our pst is:" 
   print (x) 
   putStrLn "The pst in Reverse Order is:" 
   print (reverse x)

It will produce the following output −

Our pst is:
[1,2,3,4,5,6,7,8,9,10]
The pst in Reverse Order is:
[10,9,8,7,6,5,4,3,2,1]

Length Function

This function is used to calculate the length of the pst given as an argument. Take a look at the following example −

main = do 
   let x = [1..10]   
   putStrLn "Our pst is:" 
   print (x) 
   putStrLn "The length of this pst is:" 
   print (length x)

We have 10 elements in our pst, hence our code will yield 10 as the output.

Our pst is:
[1,2,3,4,5,6,7,8,9,10]
The length of this pst is:
10

Take Function

Take function is used to create a sub-string from another String. The following code shows how you can use the take function in Haskell −

main = print(take 5 ([1 .. 10])) 

The code generates a sub-string containing 5 elements from the suppped pst −

[1,2,3,4,5]

Drop Function

This function is also used to generate a sub-string. It functions as the opposite of the take function. Look at the following piece of code −

main = print(drop 5 ([1 .. 10])) 

The code drops the first 5 elements from the suppped pst and prints the remaining 5 elements. It will produce the following output −

[6,7,8,9,10]

Maximum Function

This function is used to find the element with the maximum value from the suppped pst. Let us see how to use it in practice −

main = do 
   let x = [1,45,565,1245,02,2]   
   putStrLn "The maximum value element of the pst is:"  
   print (maximum x)

The above piece of code will generate following output −

The maximum value element of the pst is:
1245

Minimum Function

This function is used to find the element with the minimum value from the suppped pst. It’s just the opposite of the maximum function.

main = do 
   let x = [1,45,565,1245,02,2]   
   putStrLn "The minimum value element of the pst is:"  
   print (minimum x)

The output of the above code is −

The minimum value element of the pst is:
1

Sum Function

As the name suggests, this function returns the summation of all the elements present in the suppped pst. The following code takes a pst of 5 elements and returns their summation as the output.

main = do 
   let x = [1..5] 
   putStrLn "Our pst is:" 
   print (x) 
   putStrLn "The summation of the pst elements is:" 
   print (sum x)

It will produce the following output −

Our pst is:
[1,2,3,4,5]
The summation of the pst elements is:
15

Product Function

You can use this function to multiply all the elements in a pst and print its value.

main = do 
   let x = [1..5] 
   putStrLn "Our pst is:" 
   print (x) 
   putStrLn "The multippcation of the pst elements is:" 
   print (product x) 

Our code will produce the following output −

Our pst is:
[1,2,3,4,5]
The multippcation of the pst elements is: 
120

Elem Function

This function is used to check whether the suppped pst contains a specific element or not. Accordingly, it either returns a true or a false.

The following code checks whether the suppped pst of elements contains the value 786.

main = do 
   let x = [1,45,155,1785] 
   putStrLn "Our pst is:" 
   print (x) 
   putStrLn "Does it contain 786?" 
   print (elem 786 (x))

It will produce the following output −

Our pst is:
[1,45,155,1785]
Does it contain 786?
False

Use the same code to check if the suppped pst contains the value 1785 or not.

Advertisements