Haskell Program to Sort Elements in Lexicographical Order (Dictionary Order)


In this tutorial, we discuss writing a program to sort elements in lexicographical order in the Haskell programming language. Haskell is a Declarative, Strongly Typed, and Functional programming language. The computations in Haskell are mathematical functions.

The lexicographical order is a dictionary order or in the order of the ASCII values of a character.

In this tutorial, we see two different ways to implement a program to sort elements in lexicographical order in Haskell.

  • Program to sort elements in lexicographical order using built-in function sort.

  • Program to sort elements in lexicographical order using custom sorting functions.

Algorithm steps

  • Declare or take input a list of elements.

  • Implement the program to sort the elements.

  • Print or Display the sorted elements.

Example 1

Program to sort elements in lexicographical order using the function sort

import Data.List main = do -- declaring and initializing the list elements let list=["wel","bel","jel","keen","roll"] -- invoking the sort function and printing the returned result print ("The sorted List in lexicographical order is:") print (sort list)

Output

"The sorted List in lexicographical order is:"
["bel","jel","keen","roll","wel"]

In the above program, We imported the List module from the Data package for some utility functions on the list. We declared and initialized a list with some string values ("bel","jel","keen","roll","wel"). We invoked a function sort with the argument of the declared list. sort is a function in the list module that takes a list as an argument and returns the sorted list. Finally, we printed the returned sorted list.

Example 2

Program to sort elements in lexicographical order using the custom sort function

-- function declaration for function insert insert :: [[Char]]->[Char]->[[Char]] -- function definition for function insert -- base case insert [] y = [y] insert (x:xs) y = if y < x then [y]++[x]++xs else [x]++(insert xs y) -- function declaration for function insert sort :: [[Char]]->[[Char]] -- function definition for function insert -- base case sort [] = [] sort (x:xs) = insert (sort xs) x main = do -- declaring and initializing the list elements let list=["wel","bel","jel","keen","roll"] -- print ("The sorted List in lexicographical order is:") print (sort list)

Output

"The sorted List in lexicographical order is:"
["bel","jel","keen","roll","wel"]

In the above program,

  • We declared a function insert which takes an array of strings and a string as input and returns an array of strings. In its function definition, we are taking two argument list-pattern (x:xs) and a string y. We are comparing x (the first string in the list in the first argument) with y. If y is lexicographically smaller than x, we are returning the list [y]++[x]++xs. Where ‘++’ is an operator to concatenate two lists. If the y is lexicographically greater than x, we are returning x concatenated with a recursive call to the function itself with arguments remaining list and y.

  • I.e insert is a utility function that inserts elements in a sorted list at the correct position. For example for this function call insert [2,4,5] 3, the output is [2,3,4,5].

  • We declared a function sort as such it takes a list of strings as input and returns a list of strings.

  • In its function definition, we are invoking the insert where the first argument is a recursive call itself with the argument remaining list and the second argument is the first element in the list. In the base case, we are returning an empty list when the argument is an empty list. This function uses an insertion sort technique to sort elements in the list.

  • In the main function, we declared and initialized a list with some string value. We invoked the function sort with the declared list and finally, printed the returned sorted list.

Conclusion

In this tutorial, we discussed implementing a program to sort elements in lexicographical order in the Haskell programming language.

Updated on: 24-Nov-2022

401 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements