【问题标题】:Operator and operand don't agree can't find why运算符和操作数不一致 找不到原因
【发布时间】:2015-05-31 03:27:16
【问题描述】:

我正在处理此代码,但无法使其运行。我检查了几次,仍然无法弄清楚为什么它不起作用。

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

这是我尝试运行它时得到的结果:

【问题讨论】:

  • 您将get_nth 应用于一对列表和字符串,而不是列表和int:get_nth(months, Int.toString(#2 date))Int.toString 的结果不是 int。您可能根本不想在那里使用该功能。此外,还有一个基础库函数List.nth 您可能想要使用。

标签: sml ml


【解决方案1】:

当您看到表单错误时:

operator domain: <type1>
operand : <type2>

然后它说它期待 &lt;type1&gt; 类型的东西,但你给它的东西是 &lt;type2&gt; 类型的东西。在您的情况下,get_nth 需要一个元组,其中第一个元素是字符串列表,第二个元素是 int。您正在提供一个元组,其中第一个元素是字符串列表(这是正确的),但您的第二个参数是一个字符串,它应该是一个 int。你会想要改变

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

get_nth(months, #2(date))

【讨论】:

  • 好观察,我完全忘记了。谢谢
猜你喜欢
  • 2017-06-28
  • 2020-05-19
  • 1970-01-01
  • 2021-05-17
  • 2018-04-28
  • 2023-03-15
  • 2018-02-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多