EQUALOP error message with SML
I am trying to create a simple function that takes two dates of int * int * int format and returns 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 am getting this error:
hwk_1.sml: 1.53 Error: syntax error: insert EQUALOP
uncaught exception Compilation [Compilation: syntax error]]
lifted to: ../ 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 please help?
source to share
In addition to what has already been mentioned, you must also use pattern matching to decompose this 3-tuple. By doing this, you can also drop the type annotations as it is now clear that it is a 3-tuple (as for the reader, but more importantly also for 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 make it a little smarter. If you have multiple date functions, you can create a small helper function toDays
. In the example below, I just included the function isOlder
, but you can put it on the top level or inside the local
-declaration if you don't hide it
fun isOlder (date1, date2) =
let
fun toDays (y, m, d) = y * 365 + m * 30 + d
in
toDays date1 < toDays date2
end
source to share
FWIW, I got the same error on one of the other exercises in this same homework:
Error: syntax error: inserting EQUALOP
but in my case it was happening on the first line. This was confusing me because I come from Python where the error usually happens after the error.
Bottom line, and what I wanted to know is this: This error means that it cannot compile the code as written.
PS if you use let
and in
, you also need to use end
. (You don't need to use val
or let
to solve the problem is_older
- there is a way to do it with just installed logic).
source to share