I am trying to write a function, given two dates, calculates the distance (in days) between the two dates, using recursion. The function dateDistance takes two dates and uses nextDate to find the next valid date. goodDate makes sure its a valid date.
I am trying to get the result by counting recursions and returning the count. Each time the recursion happens the count variable n should increase and at the end of the recursion when it reaches the end condition (fy == sy && fm == sm && sd == fd) = n it shall return the n.
leapYear x = if ((x `mod` 4) == 0) && ((x `mod` 100) /= 0) || ((x `mod` 400) == 0)
then True
else False
goodDate (y,m,d)
| (d<1 || m>12 || m<1 || y==0) = False
| m `elem` [1,3,5,7,8,10,12] = d<32
| m `elem` [4,6,9,11] = d<31
| leapYear y = d<30
| otherwise= d<29
nextDate (y,m,d) =
if goodDate(y,m,(d+1))
then (y,m,(d+1))
else if goodDate(y,(m+1),1)
then (y,(m+1),1)
else ((y+1),1,1)
dateDistance (fy,fm,fd) (sy,sm,sd)
|(fy == sy && fm == sm && sd == fd) = n
|otherwise = dateDistance nextDate (fy,fm,fd) (sy,sm,sd)
where n = 0