Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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
<div class="execute"></div><div class="code-mirror language-haskell" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token import-statement"><span class="token keyword">import</span> Data<span class="token punctuation">.</span>List</span> <span class="token hvariable">main</span> <span class="token operator">=</span> <span class="token keyword">do</span> <span class="token comment">-- declaring and initializing the list elements</span> <span class="token keyword">let</span> <span class="token hvariable">list</span><span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">"wel"</span><span class="token punctuation">,</span><span class="token string">"bel"</span><span class="token punctuation">,</span><span class="token string">"jel"</span><span class="token punctuation">,</span><span class="token string">"keen"</span><span class="token punctuation">,</span><span class="token string">"roll"</span><span class="token punctuation">]</span> <span class="token comment">-- invoking the sort function and printing the returned result</span> <span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token string">"The sorted List in lexicographical order is:"</span><span class="token punctuation">)</span> <span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token builtin">sort</span> <span class="token hvariable">list</span><span class="token punctuation">)</span> </div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
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
<div class="execute"></div><div class="code-mirror language-haskell" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment">-- function declaration for function insert</span>
<span class="token hvariable">insert</span> <span class="token operator">::</span> <span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token constant">Char</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token operator">-></span><span class="token punctuation">[</span><span class="token constant">Char</span><span class="token punctuation">]</span><span class="token operator">-></span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token constant">Char</span><span class="token punctuation">]</span><span class="token punctuation">]</span>
<span class="token comment">-- function definition for function insert</span>
<span class="token comment">-- base case</span>
<span class="token hvariable">insert</span> <span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token hvariable">y</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token hvariable">y</span><span class="token punctuation">]</span>
<span class="token hvariable">insert</span> <span class="token punctuation">(</span><span class="token hvariable">x</span><span class="token operator">:</span><span class="token hvariable">xs</span><span class="token punctuation">)</span> <span class="token hvariable">y</span> <span class="token operator">=</span> <span class="token keyword">if</span> <span class="token hvariable">y</span> <span class="token operator"><</span> <span class="token hvariable">x</span>
<span class="token keyword">then</span> <span class="token punctuation">[</span><span class="token hvariable">y</span><span class="token punctuation">]</span><span class="token operator">++</span><span class="token punctuation">[</span><span class="token hvariable">x</span><span class="token punctuation">]</span><span class="token operator">++</span><span class="token hvariable">xs</span>
<span class="token keyword">else</span> <span class="token punctuation">[</span><span class="token hvariable">x</span><span class="token punctuation">]</span><span class="token operator">++</span><span class="token punctuation">(</span><span class="token hvariable">insert</span> <span class="token hvariable">xs</span> <span class="token hvariable">y</span><span class="token punctuation">)</span>
<span class="token comment">-- function declaration for function insert</span>
<span class="token builtin">sort</span> <span class="token operator">::</span> <span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token constant">Char</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token operator">-></span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token constant">Char</span><span class="token punctuation">]</span><span class="token punctuation">]</span>
<span class="token comment">-- function definition for function insert</span>
<span class="token comment">-- base case</span>
<span class="token builtin">sort</span> <span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
<span class="token builtin">sort</span> <span class="token punctuation">(</span><span class="token hvariable">x</span><span class="token operator">:</span><span class="token hvariable">xs</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token hvariable">insert</span> <span class="token punctuation">(</span><span class="token builtin">sort</span> <span class="token hvariable">xs</span><span class="token punctuation">)</span> <span class="token hvariable">x</span>
<span class="token hvariable">main</span> <span class="token operator">=</span> <span class="token keyword">do</span>
<span class="token comment">-- declaring and initializing the list elements</span>
<span class="token keyword">let</span> <span class="token hvariable">list</span><span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">"wel"</span><span class="token punctuation">,</span><span class="token string">"bel"</span><span class="token punctuation">,</span><span class="token string">"jel"</span><span class="token punctuation">,</span><span class="token string">"keen"</span><span class="token punctuation">,</span><span class="token string">"roll"</span><span class="token punctuation">]</span>
<span class="token comment">-- print ("The sorted List in lexicographical order is:")</span>
<span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token builtin">sort</span> <span class="token hvariable">list</span><span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
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.
