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.

import Data.Char main :: IO() main = do -- generating alphabets from A-Z as list of characters let alphabets=[chr x | x<-[65..90]] -- printing the list of Characters print ("Alphabets from A-Z are:") print alphabets

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.

import Data.Char import Data.Char -- function to print uppercase letters a-z -- function declaration printAlphabets :: Int -> IO() -- function definition -- base case printAlphabets 25 = do print ('Z') printAlphabets d = do putStr (show (chr (65+d)) ++ " ") printAlphabets (d+1) -- function to print lowercase letters a-z -- function declaration printAlphabets2 :: Int -> IO() -- function definition printAlphabets2 25 = do print ('z') printAlphabets2 d = do putStr (show (chr (97+d)) ++ " ") printAlphabets2 (d+1) main :: IO() main = do -- invoking printAlphabets function print ("Alphabets from A-Z are:") printAlphabets 0 print ("Alphabets from a-z are:") printAlphabets2 0

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: 27-Oct-2022

432 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements