Haskell Program to extract the last two digits from the given year


This tutorial will help us in extracting the last two digits from the given year. The year value is passed as an argument to the function defined and then the last two digits are extracted by using various methods in Haskell. And the final output is displayed.

For example, For entered year = 2023, the last two digits = 23.

Algorithm

  • Step 1 − The Data.Char module is imported to use digitToInt function.

  • Step 2 − The extractLastTwoDigits function is defined

  • Step 3 − Program execution will be started from main function. The main() function has whole control of the program.

  • Step 4 − The variable named, “year” is being initialized. It will contain the year value whose last two digit is to be extracted.

  • Step 5 − The final resultant last two digits of the year is displayed by using ‘putStrLn’ statement.

Using extractLastTwoDigits Function

The extractLastTwoDigits function takes an Int as an argument, which represents the year. The init function is used to remove the last digit from the year, and the last function is used to extract the last digit of the remaining digits of the year.

Example 1

import Data.Char (digitToInt)

extractLastTwoDigits :: Int -> String
extractLastTwoDigits year = [(last (init (show year))), last (show year)]

main :: IO ()
main = do
   let year = 2023
   let result = extractLastTwoDigits year
   putStrLn ( "Last two digits of the year are: " ++ result)

Output

Last two digits of the year are: 23

Using Mod Function

In this example, the mod function returns the remainder of a division, and the div function returns the quotient of a division. By dividing the year by 100 and then taking the remainder, we can get the last two digits of the year.

Example 2

import Data.Char (digitToInt)

extractLastTwoDigits :: Int -> Int
extractLastTwoDigits year = year `mod` 100

main :: IO ()
main = do
   let year = 2021
   let result = extractLastTwoDigits year
   putStrLn ( "Last two digits of the year are: " ++ show result)

Output

Last two digits of the year are: 21

Using List Comprehension

In this example, the list comprehension is used to extract the last two digits of the year by converting it to a string and then using the digitToInt function.

Example 3

import Data.Char (digitToInt)

extractLastTwoDigits :: Int -> [Int]
extractLastTwoDigits year = [digitToInt x | x <- (drop (length (show year) - 2) (show year))]

main :: IO ()
main = do
    let year = 2018
    let result = extractLastTwoDigits year
    putStrLn ( "Last two digits of the year are: " ++ show result)

Output

Last two digits of the year are: [1,8]

Conclusion

In Haskell, there are various ways to extract the last two digits of the year entered. To extract the last two digits of the year we can use, user-defined extractLastTwoDigits function, mod function or list comprehension. The year value is passed as an argument to these functions and then the last two digits of the year are extracted.

Updated on: 23-Jan-2023

281 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements