I am trying to create a simple function that takes two dates of format int*int*int and return if the first one is older than the second or not.
fun is_older (date1: (int*int*int), date2: (int*int*int)) =
val in_days1 = (#1 (date1) * 365) + (#2 (date1) * 30) + #3 date1;
val in_days2 = (#1 (date2) * 365) + (#2 (date2) * 30) + #3 date1;
if in_days1 < in_days2
then true
else false
I get this error:
hwk_1.sml:1.53 Error: syntax error: inserting EQUALOP
uncaught exception Compile [Compile: "syntax error"]
raised at: ../compiler/Parse/main/smlfile.sml:15.24-15.46
../compiler/TopLevel/interact/evalloop.sml:44.55
../compiler/TopLevel/interact/evalloop.sml:296.17-296.20
Can anyone help please?
In addition to what has already been mentioned, you also ought to use pattern matching to decompose that 3-tuple. Doing this, you can also throw away the type annotations, as it is now clear that this is a 3-tuple (both for the reader, but more importantly also the type system).
fun is_older ((y1, m1, d1), (y2, m2, d2)) =
let
val days1 = y1 * 365 + m1 * 30 + d1
val days2 = y2 * 365 + m2 * 30 + d2
in
days1 < days2
end
However you could do this a bit smarter. If you have multiple functions working with dates, you could create a nice little helper function toDays
. In the below example i have just included inside the isOlder
function, but you could put it at top level or inside a local
-declaration if you wan't to hide it away
fun isOlder (date1, date2) =
let
fun toDays (y, m, d) = y * 365 + m * 30 + d
in
toDays date1 < toDays date2
end
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