How to resolve the algorithm The Twelve Days of Christmas step by step in the Haskell programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm The Twelve Days of Christmas step by step in the Haskell programming language

Table of Contents

Problem Statement

Write a program that outputs the lyrics of the Christmas carol The Twelve Days of Christmas. The lyrics can be found here.
(You must reproduce the words in the correct order, but case, format, and punctuation are left to your discretion.)

Let's start with the solution:

Step by Step solution about How to resolve the algorithm The Twelve Days of Christmas step by step in the Haskell programming language

Explanation:

This Haskell code defines a program that generates and prints the verses of the Christmas carol "The Twelve Days of Christmas."

1. Gifts List:

gifts :: [String]
gifts = [
 "And a partridge in a pear tree!",
 "Two turtle doves,",
 "Three french hens,",
 "Four calling birds,",
 "FIVE GOLDEN RINGS,",
 "Six geese a-laying,",
 "Seven swans a-swimming,",
 "Eight maids a-milking,",
 "Nine ladies dancing,",
 "Ten lords a-leaping,",
 "Eleven pipers piping,",
 "Twelve drummers drumming"
]

This is a list of the gifts mentioned in the song, from "And a partridge in a pear tree!" to "Twelve drummers drumming."

2. Days List:

days :: [String]
days = [
 "first",
 "second",
 "third",
 "fourth",
 "fifth",
 "sixth",
 "seventh",
 "eighth",
 "ninth",
 "tenth",
 "eleventh",
 "twelfth"
]

This list contains the days of Christmas, from the "first" day to the "twelfth" day.

3. Verse of the Day Function:

verseOfTheDay :: Int -> IO ()
verseOfTheDay day = do
 putStrLn $
   "On the " <> days !! day
     <> " day of Christmas my true love gave to me... "
 mapM_ putStrLn [dayGift day d | d <- [day, day -1 .. 0]]
 putStrLn ""
 where
   dayGift 0 _ = "A partridge in a pear tree!"
   dayGift _ gift = gifts !! gift

This function prints the verse of the song for a specific day. It takes an integer day as its input, representing the day of Christmas (from 0 to 11, inclusive).

  • It prints the beginning of the verse, which is "On the day of Christmas my true love gave to me..."
  • It uses the mapM_ function to print each gift for that day and the previous days, in reverse order (from the current day to the first day).
  • It uses a where clause to define two helper functions:
    • dayGift 0 _: This function returns "A partridge in a pear tree!" for the first day (day 0).
    • dayGift _ gift: This function returns the gift at the specified index (gift) from the gifts list for any other day.

4. Main Function:

main :: IO ()
main = mapM_ verseOfTheDay [0 .. 11]

The main function calls the verseOfTheDay function for each day, from the first day (day 0) to the twelfth day (day 11), using the mapM_ function. This prints the complete song, one verse at a time.

Overall:

This program generates and prints the verses of the Christmas carol "The Twelve Days of Christmas" in order, from the first day to the twelfth day. It uses separate lists for the gifts and the days and a helper function to determine which gifts to print for each verse.

Source code in the haskell programming language

gifts :: [String]
gifts =
  [ "And a partridge in a pear tree!",
    "Two turtle doves,",
    "Three french hens,",
    "Four calling birds,",
    "FIVE GOLDEN RINGS,",
    "Six geese a-laying,",
    "Seven swans a-swimming,",
    "Eight maids a-milking,",
    "Nine ladies dancing,",
    "Ten lords a-leaping,",
    "Eleven pipers piping,",
    "Twelve drummers drumming,"
  ]

days :: [String]
days =
  [ "first",
    "second",
    "third",
    "fourth",
    "fifth",
    "sixth",
    "seventh",
    "eighth",
    "ninth",
    "tenth",
    "eleventh",
    "twelfth"
  ]

verseOfTheDay :: Int -> IO ()
verseOfTheDay day = do
  putStrLn $
    "On the " <> days !! day
      <> " day of Christmas my true love gave to me... "
  mapM_ putStrLn [dayGift day d | d <- [day, day -1 .. 0]]
  putStrLn ""
  where
    dayGift 0 _ = "A partridge in a pear tree!"
    dayGift _ gift = gifts !! gift

main :: IO ()
main = mapM_ verseOfTheDay [0 .. 11]


  

You may also check:How to resolve the algorithm Mastermind step by step in the Raku programming language
You may also check:How to resolve the algorithm Price fraction step by step in the Elixir programming language
You may also check:How to resolve the algorithm Descending primes step by step in the Picat programming language
You may also check:How to resolve the algorithm Arithmetic/Complex step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm String concatenation step by step in the PureBasic programming language