Haskell program to print ascii values


In this tutorial, we discuss writing a program to print ASCII values in the Haskell programming language. Haskell is a Declarative, Strongly Typed, and Functional programming language. The computations in Haskell are mathematical functions.

ASCII stands for American Standard Code for Information Interchange, used in electronic data transfer and communication. These ASCII values represent characters in electronic devices. These values are used to describe characters and data in digital devices. ASCII is a 7-bit number representing a character. Example: 7-bit binary “1100001” represents character ‘a’ whose ASCII value is 97.

In this tutorial, we see two different ways to print ASCII values in Haskell and applications.

  • Haskell program to print ASCII values of a Character.

  • Haskell program to print ASCII values of a String.

Syntax

Haskell as default doesn’t provide utility functions to print ASCII values, we need to import the Char module from the Data package for utility function on characters.

The syntax to import a module is

import Data.Char

On importing we have access to all the utility functions in the Char module

We use the function ord to get the ASCII value of a Character. ord is a function that takes a character as an argument the returns the ASCII value of that character.

Syntax to use function ord is −

Output for function call ord ‘a’ is 97. As the ASCII value of character ‘a’ is 97.

Algorithm steps

  • Declare or take input for which ASCII value is to be calculated.

  • Implement the logic to print the ASCII value.

  • Print or Display the computed ASCII value.

Example 1

Haskell program to print ASCII value of a Character

-- import the Char module import Data.Char main :: IO() main = do -- initializing variable ch with a character let ch='c' -- printing the ASCII value of tha character print ("The ASCII value of the character c is:") print (ord ch)

Output

"The ASCII value of the character c is:"
99

In the above program, we imported the module Char which has utility functions for operations on characters. In the main function, we declared and initialized variable ch with the character value ‘c’. We invoked the function ord which argument as variable ch, which is a utility function in the Char module that takes a character as an argument and returns the ASCII value of that character. Finally, we printed the returned result using the print function.

Example 2

Haskell program to print ASCII values of a String

-- import the Char module import Data.Char -- function declaration printAscii :: [Char]->IO() -- function definition printAscii [ch] = do print (ord ch) printAscii (ch:str) = do print (ord ch) printAscii (str) main :: IO() main = do -- initializing the variable str with a character let str="hello" -- invoking the function printAscii print ("The ASCII values of the String hello is:") printAscii str

Output

"The ASCII values of the String hello is:"
104
101
108
108
111

In the above program, we initially imported utility module, Char. We declared a function printAscii as such it takes String as an argument and returns IO actions. In the function definition, we are taking a list of characters as input. In the base conditions for which array a single character we printed the ASCII value of that character using the utility function ord. For all other cases, we defined functions as it accepts the argument using a string pattern. We are isolating the first element of the string using a string pattern. We printed the ASCII value of the first character and we recursively called the function printAscii for which the argument is the remaining string. The logic is we are printing the Ascii value of the first character recursively until the string becomes empty. In the main function, we declared and initialized variable str with the value “hello” and finally we invoked the function with str as an argument that prints the ASCII values of the string.

Conclusion

In this tutorial, we discussed two different ways to print ASCII values in the Haskell programming language.

Updated on: 24-Nov-2022

505 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements