Search code examples
tuplessml

Are tuples comparable in SML?


Suppose we needed to compare values inside tuples (to give context, let's say a tuple of 3 values represents a data dd/mm/yy) & we want to check which if date1< date2.

Is it possible to compare tuples?

fun is_older(date1:int*int*int, date2:int*int*int) =
 (#3 date1, #2 date1, #1 date1) < (#3 date2, #2 date2, #1 date2);

This answer was generated by chatGPT & gave an error. I correctly implemented a similar function using if statements, but was wondering if tuples are actually comparable.


Solution

  • Again, I suggest pattern matching. It's cleaner.

    fun is_older((a, b, c), (x, y, z)) =
     (c, b, a) < (z, y, x);
    

    Second, no. The type of op< is int * int → bool.