Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A day on the calendar (usual Gregorian calendar used in the USA) can be represen

ID: 3747376 • Letter: A

Question

A day on the calendar (usual Gregorian calendar used in the USA) can be represented as a tuple with three Int values (month,day,year) where the year is a positive integer, 1 <= month <= 12, and 1 <= day <= days_in_month. Here days_in_month is the number of days in the the given month (i.e. 28, 29, 30, or 31) for the given year.

Develop a Boolean Haskell function validDay d that takes a date tuple d and returns True if and only if d represents a valid date.

For example, validDay (8,20,2018) and validDay(2,29,2016} yield True and validDay (2,29,2017) and validDay(0,0,0) yield False.

Note: The Gregorian calendar was introduced by Pope Gregory of the Roman Catholic Church in October 1582. It replaced the Julian calendar system, which had been instituted in the Roman Empire by Julius Caesar in 46 BC. The goal of the change was to align the calendar year with the astronomical year.

Some countries adopted the Gregorian calendar at that time. Other countries adopted it later. Some countries may never have adopted it officially.

However, the Gregorian calendar system became the common calendar used worldwide for most civil matters. The proleptic Gregorian calendar extends the calendar backward in time from 1582. The year 1 BC becomes year 0, 2 BC becomes year -1, etc. The proleptic Gregorian calendar underlies the ISO 8601 standard used for dates and times in software systems.

Explanation / Answer

This code is written in Haskell language as asked.

It checks three things :

1) Day, Month and Year is greater than 0

2) If not February, then day <= days_in_month should hold true

3) If Februray and leap year - day<=29 and day<=28 if not a leap year.

Haskell Code:

validDay :: (Integer,Integer,Integer) -> Bool   
greaterThanZero :: (Integer,Integer,Integer) -> Bool
isLeapYear :: Integer -> Bool
notFebDaysValid :: (Integer,Integer,Integer) -> Bool
febDaysValid :: (Integer,Integer,Integer) -> Bool


greaterThanZero (y,x,z) =  
if (x>0 && y>0 && z>0) then True
else False

isLeapYear y
| isDivBy 400 = True
| isDivBy 100 = False
| isDivBy 4 = True
| otherwise = False
where isDivBy a = mod y a == 0


notFebDaysValid (y,x,z) =
if (y/=2)
then if y==1 && x<=31 then True
else if y==3 && x<=31 then True
else if y==5 && x<=31 then True
else if y==7 && x<=31 then True
else if y==8 && x<=31 then True
else if y==10 && x<=31 then True
else if y==12 && x<=31 then True
else if y==4 && x<=30 then True
else if y==6 && x<=30 then True
else if y==9 && x<=30 then True
else if y==11 && x<=30 then True
else if y==11 && x<=30 then True
else if y==3 && x<=30 then True
else False
else True
  
febDaysValid (y,x,z) =
if (y==2)
then if isLeapYear z ==True && x<=29 then True
else if isLeapYear z ==False && x<=28 then True
else False
else True

validDay (x,y,z) =  
if greaterThanZero(x,y,z) && notFebDaysValid(x,y,z) && febDaysValid(x,y,z) then True
else False

main = do
putStrLn "The addition of the two numbers is:"  
print(validDay (2,29,2017))