4
votes

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?

4
You can replace your if-then-else just with the condition, in_days1 < in_days2.Michael J. Barber

4 Answers

6
votes

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
5
votes
val in_days1 = (#1 (date1) * 365) + (#2 (date1) * 30) + #3 date1;
val in_days2 = (#1 (date2) * 365) + (#2 (date2) * 30) + #3 date1;

Local val definitions need to be between let and in.

0
votes

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 to me, because I'm coming from Python, where the error usually happens after the mistake.

Bottom line, and the thing I wanted to know is this: this error means it can't compile the code as written.

P.S. if you use let and in you also have to use end. (You didn't need to use val or let to solve the is_older problem - there is a way to do it with set logic alone).

0
votes

it worked for me:

fun is_older(diaUno : (int * int * int), diaDos : (int * int * int)) =

    let

        fun setDayNum(diaUno : (int * int * int)) =

            let
                val diaUnoInt = (#1 (diaUno) * 365) + (#2 (diaUno) * 30) + #3 diaUno
            in
                diaUnoInt
            end 

        val dia1 = setDayNum(diaUno)
        val dia2 = setDayNum(diaDos)

    in

        if dia1 < dia2 then diaUno else diaDos

    end