EQUALOP error message with SML

14,004

Solution 1

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

Solution 2

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.

Share:
14,004
Augusto Dias Noronha
Author by

Augusto Dias Noronha

Updated on June 14, 2022

Comments

  • Augusto Dias Noronha
    Augusto Dias Noronha almost 2 years

    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?