Updated April 11, 2023
Introduction to Haskell Sort
Whenever we want to sort the elements of a given list, then we make use of the sort function in Haskell programming language, and the name of the list consisting of elements that are to be sorted is passed as a parameter to the sort function and the sort function takes a list as the input and returns the sorted list as the output and the sort function sorts the elements of the given list in ascending order and returns it as a new list and the sort function is available in Data.List module which must be imported in our program to make use of sort function and the internal implementation of the sort function is merge sort.
The syntax to define sort function in Haskell is as follows:
import Data.List
sort list_name
where Data. List is the module to be imported to use the sort function and
list_name is the name of the list to be sorted.
How to work sort function in Haskell?
Working of sort function in Haskell is as follows
- Whenever we want to sort the elements of a given list, then we make use of the sort function in the Haskell programming language.
- The name of the list consisting of elements that are to be sorted is passed as a parameter to the sort function.
- The sort function takes a list as the input and returns the sorted list as the output.
- The sort function sorts the elements of the given list in ascending order and returns it as a new list.
- The sort function is available in Data. List module which must be imported in our program to make use of sort function.
- The internal implementation of the sort function is merge sort.
Examples
Let us discuss examples of Haskell Sort.
Example #1
Haskell program to demonstrate sort function using which we are going to sort the elements of a given list in ascending order and display the resulting new list as the output on the screen:
Code:
--importing Data. List module to be able to use sort function
import Data.List
--defining a list to store integer values
a :: [Integer]
a = [5,3,7,4]
--defining another list to store the resulting list after using sort function to sort the elements in ascending order
b :: [Integer]
b = sort a
--displaying the resulting list after using sort function as the output on the screen
main = do
putStrLn "The resulting list consisting of elements sorted in ascending order is:\n"
print $ b
The output of the above program is as shown in the snapshot below:
In the above program, we are importing Data. List module in our program to be able to make use of sort function. Then we are defining a list to store integer values. Then we are making use of the sort function on the list containing integer values to sort them in ascending order which returns a new list consisting of elements sorted in ascending order. Then we are defining another list to store the sorted list. Then we are displaying the sorted list as the output on the screen.
Example #2
Haskell program to demonstrate sort function using which we are going to sort the elements of a given list in ascending order and then reverse the resulting list to display the elements of the list in descending order and display the resulting lists as the output on the screen:
Code:
--importing Data. List module to be able to use sort function
import Data.List
--defining a list to store integer values
a :: [Integer]
a = [5,3,7,4]
--defining another list to store the resulting list after using sort function to sort the elements in ascending order
b :: [Integer]
b = sort a
--defining another list to store the resulting list after reversing the sorted list to display the elements in descending order
c :: [Integer]
c = reverse b
--displaying the resulting list after using sort function and reverse function as the output on the screen
main = do
putStrLn "The resulting list consisting of elements sorted in ascending order is:\n"
print $ b
putStrLn "\n"
putStrLn "The resulting list consisting of elements sorted in descending order is:\n"
print $ c
The output of the above program is as shown in the snapshot below:
In the above program, we are importing Data. List module in our program to be able to make use of sort function. Then we are defining a list to store integer values. Then we are making use of the sort function on the list containing integer values to sort them in ascending order which returns a new list consisting of elements sorted in ascending order. Then we are defining another list to store the sorted list. Then we are displaying the sorted list as the output on the screen. Then we are using the reverse function on the resulting sorted list in ascending order to display the elements in descending order.
Example #3
Haskell program to demonstrate sort function using which we are going to sort the elements of a given list in ascending order and then reverse the resulting list to display the elements of the list in descending order and display the resulting lists as the output on the screen:
Code:
--importing Data. List module to be able to use sort function
import Data.List
--defining a list to store integer values
a :: [Integer]
a = [20, 10, 50, 30]
--defining another list to store the resulting list after using sort function to sort the elements in ascending order
b :: [Integer]
b = sort a
--defining another list to store the resulting list after reversing the sorted list to display the elements in descending order
c :: [Integer]
c = reverse b
--displaying the resulting list after using sort function and reverse function as the output on the screen
main = do
putStrLn "The resulting list consisting of elements sorted in ascending order is:\n"
print $ b
putStrLn "\n"
putStrLn "The resulting list consisting of elements sorted in descending order is:\n"
print $ c
The output of the above program is as shown in the snapshot below:
In the above program, we are importing Data. List module in our program to be able to make use of sort function. Then we are defining a list to store integer values. Then we are making use of sort function on the list containing integer values to sort them in ascending order which returns a new list consisting of elements sorted in ascending order. Then we are defining another list to store the sorted list. Then we are displaying the sorted list as the output on the screen. Then we are using a reverse function on the resulting sorted list in ascending order to display the elements in descending order.
Conclusion
In this article, we have learned the concept of sort through definition, syntax, and working of a sort with corresponding programming examples and their outputs to demonstrate them.
Recommended Articles
We hope that this EDUCBA information on “Haskell Sort” was beneficial to you. You can view EDUCBA’s recommended articles for more information.