【问题标题】:SMLcircularity Error of operand and operator don't agree操作数和运算符的 SMLcircularity Error 不一致
【发布时间】:2023-03-15 06:31:01
【问题描述】:
    fun insert R x [] acc = [x]
  | insert R x (h::t) acc =
    if R (x,h) then acc::(x::(h::t))
    else(
        acc=acc::h;
        insert R x t acc
        );
fun isort_aux R [] acc = acc
  | isort_aux R (x::xs) acc =
    isort_aux xs (insert R x acc [])
fun isort_2 R xs = isort_aux R xs []

我正在尝试在 sml 中编写用于插入排序的尾递归代码,为此我创建了一个累加器“acc”,但在第 5 行 acc=acc::h 出现以下错误

Standard ML of New Jersey v110.78 [built: Thu Aug 31 03:45:42 2017]
- stdIn:5.3-5.13 Error: operator and operand don't agree [circularity]
  operator domain: 'Z * 'Z list
  operand:         'Z * 'Z
  in expression:
    acc :: h
- 

【问题讨论】:

  • acc = acc::h 是一个比较,你不能将hacc 放在一起。您可能应该复习 SML 的基础知识,并从明确写下类型开始。

标签: error-handling compiler-errors smlnj


【解决方案1】:

acc=acc::h; 插入 R x t acc

这里有两个调用在 SML 中不起作用。由于语言的结构方式,您只能在 SML 中做一件事。几周前我在开始使用 SML 时这样做了,但它对我不起作用。

正如@moldbnilo 的评论所说,您的代码正在尝试将 acc 与 acc::h 进行比较,而不是将其设置为 acc。

我对 SML 非常陌生,因此请谨慎对待,因为它可能不正确。

【讨论】:

    猜你喜欢
    • 2017-06-28
    • 2020-05-19
    • 1970-01-01
    • 2021-05-17
    • 2018-04-28
    • 1970-01-01
    • 2018-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多