How to resolve the algorithm Last Friday of each month step by step in the Haskell programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Last Friday of each month step by step in the Haskell programming language

Table of Contents

Problem Statement

Write a program or a script that returns the date of the last Fridays of each month of a given year. The year may be given through any simple input method in your language (command line, std in, etc).

Example of an expected output:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Last Friday of each month step by step in the Haskell programming language

This Haskell code defines functions to find the dates for a specific weekday (Monday in this case) in a given year and then prints the results for multiple years. Here's a detailed explanation of the code:

  1. Importing Modules:

    import Data.Time.Calendar
    import Data.Time.Calendar.WeekDate
    import Data.List

    This code imports necessary modules for working with dates, week dates, and list operations.

  2. findWeekDay Function:

    findWeekDay :: Int -> Day -> Day

    This function takes two arguments: dayOfWeek (an integer representing the day of the week, where 1 is Monday and 7 is Sunday) and date (a Day value representing a specific date). It returns a Day value representing the date for the given dayOfWeek in the same week as the input date.

  3. weekDayDates Function:

    weekDayDates :: Int -> Integer -> [String]

    This function takes two arguments: dayOfWeek (as in findWeekDay) and year (an integer representing a year). It returns a list of strings, where each string represents a date in the given year for the specified dayOfWeek.

  4. Main Function:

    main :: IO ()

    This is the main function of the program.

  5. mapM_ Function:

    mapM_ putStrLn

    This function applies the putStrLn function (which prints a string followed by a newline) to each element of a list. In this case, it prints each string in the list created by the following expression.

  6. transpose and intercalate Functions:

    intercalate "  " <$> transpose

    The transpose function converts a list of lists into a list of lists with the elements in each inner list transposed. The intercalate function joins a list of strings into a single string using a specified separator (here, " ").

  7. Generating Dates:

    (weekDayDates 5 <$> [2012 .. 2017])

    This expression generates a list of lists of strings, where each inner list contains the dates for Monday (day of the week 5) in the years 2012 through 2017.

  8. Printing Results:

    mapM_ putStrLn

    This expression applies putStrLn to each list of dates, printing each list on a separate line.

Source code in the haskell programming language

import Data.Time.Calendar
       (Day, addDays, showGregorian, fromGregorian, gregorianMonthLength)
import Data.Time.Calendar.WeekDate (toWeekDate)
import Data.List (transpose, intercalate)

-- [1 .. 7] for [Mon .. Sun]
findWeekDay :: Int -> Day -> Day
findWeekDay dayOfWeek date =
  head
    (filter
       (\x ->
           let (_, _, day) = toWeekDate x
           in day == dayOfWeek)
       ((`addDays` date) <$> [-6 .. 0]))

weekDayDates :: Int -> Integer -> [String]
weekDayDates dayOfWeek year =
  ((showGregorian . findWeekDay dayOfWeek) .
   (fromGregorian year <*> gregorianMonthLength year)) <$>
  [1 .. 12]

main :: IO ()
main =
  mapM_
    putStrLn
    (intercalate "  " <$> transpose (weekDayDates 5 <$> [2012 .. 2017]))


  

You may also check:How to resolve the algorithm Hello world/Newbie step by step in the C++ programming language
You may also check:How to resolve the algorithm Long multiplication step by step in the Dc programming language
You may also check:How to resolve the algorithm Quine step by step in the Modula-2 programming language
You may also check:How to resolve the algorithm Find the intersection of two lines step by step in the Go programming language
You may also check:How to resolve the algorithm Prime decomposition step by step in the PowerShell programming language