I'm working on a small haskell program that I gave the name the "Haskel-dator", hahaha....anyway, heres my current progress on the code for it, and the run-time errors that followed:
Code
data Date = Date Int Int Int
type Date = (Int,Int,Int)
sum :: Int -> Int -> Int
sum x y | let x - y = z
| where z > 0
diff :: Date -> Date -> Int -- Difference between the two dates
diff (a,b,c) (x,y,z) = if a < x && b < y && c < z
then do sum
else return 0
Errors
*ERROR "myprogram.hs": - Multiple declarations of type constructor "Date"
*
Basically, I want my program to have the following assumptions:
I want my program to achieve an output that should look something like this:
Example Output
diff (1,1,2010) (10,1,2010) => 9
diff (2,2,2011) (2,2,2012) => 365
diff (28,2,2012) (1,3,2012) => 2
Any ideas?
You should look at the standard time package.
The diffDays function is exactly what you want. It returns the number of days between two dates.
diffDays :: Day -> Day -> Integer
The dates are defines with as Day.
Day instances can be build by using the fromGregorian function.
The complete example can be found here.
Keep in mind that calculating the number of days between two dates correctly is a very complex task.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With