Search code examples
smlml

Operator and operand don't agree can't find why


I'm working on this code and can't make it run. I've checked it few times and still can't figure out why it's not working.

fun date_to_string (date : (int * int * int)) =
  let
      val months = ["January", "February","March", "April",
                    "May", "June","July", "August", "September", "October", "November", "December"];
      fun get_nth (xs : string list, n : int) =
        if n=1
        then hd xs
        else get_nth(tl xs, n-1)
  in
      get_nth(months, Int.toString(#2 date)) ^ " " ^ Int.toString(#3 date) ^ ", " ^ Int.toString(#1 date)
  end

Here is what I get back when I try to run it:

enter image description here


Solution

  • When you see an error of the form:

    operator domain: <type1>
    operand : <type2>
    

    then it is saying that it is expecting something of type <type1>, but you are giving it something of type <type2>. In your case, get_nth is expecting a tuple, where the first element is a list of strings and the second element is an int. You are providing a tuple where the first element is a list of strings (which is correct), but your second argument is a string, which should be an int. You are going to want to change

    get_nth(months, Int.toString(#2 date))
    

    to

    get_nth(months, #2(date))