Haskell program to display alphabets (A-Z) using a loop

This tutorial will discuss writing a program to display characters from A-Z in Haskell programming Language. Haskell is a Functional, Declarative, and Strongly Typed Language. The computations in Haskell are mathematical functions.

In this tutorial, We see two ways to display Alphabets in Haskell.

  • An iterative Program to display Alphabets.

  • A recursive Program to display Alphabets.

NoteĀ ? Haskell doesn?t support Loops, So we mimic the function of a loop with other iterative and recursive implementations.

Algorithmic Steps

  • Implement the logic for printing Alphabets.

  • Print/display Alphabets.

Displaying Alphabets In An Iterative Way Using List Comprehensions.

Example

Program to display alphabets in an iterative way using list comprehensions.

<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>Char</span>

<span class="token hvariable">main</span> <span class="token operator">::</span> <span class="token constant">IO</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token hvariable">main</span> <span class="token operator">=</span> <span class="token keyword">do</span>
<span class="token comment">-- generating alphabets from A-Z as list of characters</span>
   <span class="token keyword">let</span> <span class="token hvariable">alphabets</span><span class="token operator">=</span><span class="token punctuation">[</span><span class="token builtin">chr</span> <span class="token hvariable">x</span> <span class="token operator">|</span> <span class="token hvariable">x</span><span class="token operator"><-</span><span class="token punctuation">[</span><span class="token number">65</span><span class="token operator">..</span><span class="token number">90</span><span class="token punctuation">]</span><span class="token punctuation">]</span>
<span class="token comment">-- printing the list of Characters</span>
   <span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token string">"Alphabets from A-Z are:"</span><span class="token punctuation">)</span>
   <span class="token builtin">print</span> <span class="token hvariable">alphabets</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

"Alphabets from A-Z are:"
"A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"

Note ? spacing is given to differentiate letter. There is no space between characters in the actual output. The list of characters is String.

In the above program, we imported Char module as by default all functions are not available for the User. The syntax for importing modules is import Data.MODULE_NAME. In the main function, we are generating a list of characters for A-Z using list comprehension. In that, we are generating integers from 65 to 90. As we know the Ascii values of the UpperCase letter range from 65 to 95. After generating the numbers we are invoking the chr function with generated number as a parameter and loading them into a list. Where chr is a function available in the Char module that takes input an integer (ASCII value) and converts it into a Character corresponding to the ASCII value of the number. If the input number is 66 as the ASCII value of ?B? is 66 ?B? is returned.

Displaying Alphabets In A Recursive Way Using A Recursive Function

Example

Program to display alphabets in a recursive way using a recursive 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 import-statement"><span class="token keyword">import</span> Data<span class="token punctuation">.</span>Char</span>
<span class="token import-statement"><span class="token keyword">import</span> Data<span class="token punctuation">.</span>Char</span>
<span class="token comment">-- function to print uppercase letters a-z</span>
<span class="token comment">-- function declaration </span>
<span class="token hvariable">printAlphabets</span> <span class="token operator">::</span> <span class="token constant">Int</span> <span class="token operator">-></span> <span class="token constant">IO</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">-- function definition</span>
<span class="token comment">-- base case</span>
<span class="token hvariable">printAlphabets</span> <span class="token number">25</span> <span class="token operator">=</span> <span class="token keyword">do</span>
                    <span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token char string">'Z'</span><span class="token punctuation">)</span>  
<span class="token hvariable">printAlphabets</span>  <span class="token hvariable">d</span> <span class="token operator">=</span> <span class="token keyword">do</span>
                    <span class="token builtin">putStr</span> <span class="token punctuation">(</span><span class="token builtin">show</span> <span class="token punctuation">(</span><span class="token builtin">chr</span> <span class="token punctuation">(</span><span class="token number">65</span><span class="token operator">+</span><span class="token hvariable">d</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">++</span> <span class="token string">" "</span><span class="token punctuation">)</span>
                    <span class="token hvariable">printAlphabets</span> <span class="token punctuation">(</span><span class="token hvariable">d</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span>
<span class="token comment">-- function to print lowercase letters a-z</span>
<span class="token comment">-- function declaration </span>
<span class="token hvariable">printAlphabets2</span> <span class="token operator">::</span> <span class="token constant">Int</span> <span class="token operator">-></span> <span class="token constant">IO</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">-- function definition</span>
<span class="token hvariable">printAlphabets2</span> <span class="token number">25</span> <span class="token operator">=</span> <span class="token keyword">do</span>
                    <span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token char string">'z'</span><span class="token punctuation">)</span>       
<span class="token hvariable">printAlphabets2</span> <span class="token hvariable">d</span>  <span class="token operator">=</span> <span class="token keyword">do</span>
                    <span class="token builtin">putStr</span> <span class="token punctuation">(</span><span class="token builtin">show</span> <span class="token punctuation">(</span><span class="token builtin">chr</span> <span class="token punctuation">(</span><span class="token number">97</span><span class="token operator">+</span><span class="token hvariable">d</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">++</span> <span class="token string">" "</span><span class="token punctuation">)</span>
                    <span class="token hvariable">printAlphabets2</span> <span class="token punctuation">(</span><span class="token hvariable">d</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span>
<span class="token hvariable">main</span> <span class="token operator">::</span> <span class="token constant">IO</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token hvariable">main</span> <span class="token operator">=</span> <span class="token keyword">do</span>
<span class="token comment">-- invoking printAlphabets function</span>
        <span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token string">"Alphabets from A-Z are:"</span><span class="token punctuation">)</span>
        <span class="token hvariable">printAlphabets</span> <span class="token number">0</span>
        <span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token string">"Alphabets from a-z are:"</span><span class="token punctuation">)</span>
        <span class="token hvariable">printAlphabets2</span> <span class="token number">0</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

"Alphabets from A-Z are:"
'A' 'B' 'C' 'D' 'E' 'F' 'G' 'H' 'I' 'J' 'K' 'L' 'M' 'N' 'O' 'P' 'Q' 'R' 'S' 'T' 'U' 'V' 'W' 'X' 'Y' 'Z'

"Alphabets from a-z are:"
'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o' 'p' 'q' 'r' 's' 't' 'u' 'v' 'w' 'x' 'y' 'z'

In the above program, we imported Char module for useful function on characters. We implemented the function printAlphabets to print uppercase letters from A-Z. We declared this function such that it takes input an integer returns IO. In the function definition, this function takes an integer d as the initial offset. It adds the offset to 65 as ASCII values of uppercase alphabets start from 65. We used an inbuilt function in the Char module which takes the ASCII value and returns a character corresponding to that ASCII number. We are printing the returned character using show function which coverts character to string as putStr function print only strings. Finally we are recursively calling the same function until it meet base case i.e d = 25. Similarly we implemented printAlphabets2 to print lowercase alphabets where the offset is 97 because ASCII values of lowercase letters start from 97.

Conclusion

In this tutorial we discussed two different ways to write a program to display Alphabets from (A-Z) in Haskell programming Language.

Updated on: 2022-10-27T05:54:09+05:30

770 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements