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 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
<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">-- import the Char module</span> <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">-- initializing variable ch with a character</span> <span class="token keyword">let</span> <span class="token hvariable">ch</span><span class="token operator">=</span><span class="token char string">'c'</span> <span class="token comment">-- printing the ASCII value of tha character</span> <span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token string">"The ASCII value of the character c is:"</span><span class="token punctuation">)</span> <span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token builtin">ord</span> <span class="token hvariable">ch</span><span class="token punctuation">)</span> </div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
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
<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">-- import the Char module</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 declaration</span>
<span class="token hvariable">printAscii</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 constant">IO</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">-- function definition</span>
<span class="token hvariable">printAscii</span> <span class="token punctuation">[</span><span class="token hvariable">ch</span><span class="token punctuation">]</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 builtin">ord</span> <span class="token hvariable">ch</span><span class="token punctuation">)</span>
<span class="token hvariable">printAscii</span> <span class="token punctuation">(</span><span class="token hvariable">ch</span><span class="token operator">:</span><span class="token hvariable">str</span><span class="token punctuation">)</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 builtin">ord</span> <span class="token hvariable">ch</span><span class="token punctuation">)</span>
<span class="token hvariable">printAscii</span> <span class="token punctuation">(</span><span class="token hvariable">str</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">-- initializing the variable str with a character</span>
<span class="token keyword">let</span> <span class="token hvariable">str</span><span class="token operator">=</span><span class="token string">"hello"</span>
<span class="token comment">-- invoking the function printAscii</span>
<span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token string">"The ASCII values of the String hello is:"</span><span class="token punctuation">)</span>
<span class="token hvariable">printAscii</span> <span class="token hvariable">str</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
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.
