【问题标题】:SML how to explicitly set a function parameter type to IntInfSML 如何将函数参数类型显式设置为 IntInf
【发布时间】:2014-12-08 01:39:29
【问题描述】:

我正在尝试为 SML 中的唯一大数创建一个排序函数,但编译器一直将我的函数类型设置为 int 而不是 'a.如何明确告诉编译器使用 IntInf?

这是我的代码:

fun selectsort([a]) = [a]
  | selectsort(h::t) =
  if (hd(selectsort(t))) < h then hd(selectsort(t))::h::tl(selectsort(t))
  else h::selectsort(t);

当我尝试时

fun selectsort([a]) = [a]
  | selectsort(l : IntInf list) =
  if (hd(selectsort(tl(l)))) < hd(l) then hd(selectsort(tl(l)))::h::tl(selectsort(tl(l)))
  else hd(l)::selectsort(tl(l));

它一直给我“错误:未绑定类型构造函数:IntInf”

【问题讨论】:

  • 使用解决方法解决:if (hd(selectsort(t)) + IntInf.fromInt(0)) &lt; h + IntInf.fromInt(0) then hd(selectsort(t))::h::tl(selectsort(t))

标签: sorting sml largenumber selection-sort


【解决方案1】:

IntInf是模块的名称,类型命名为IntInf.int。唉,您的代码有些简化:

fun selectsort([a]) = [a]
  | selectsort(x::y::t : IntInf.int list) =
    if y < x then y::x::selectsort(t) else x::selectsort(y::t)

但是请注意,IntInf 是一个可选模块,并非在所有实现中都可用。 (另外,您应该为空列表添加一个案例。)

【讨论】:

  • 我为一段非常具体的代码做了这个,所以空列表不会被排序。
  • @Jean-LucNacifCoelho,无论如何添加缺失的案例并制作它们被认为是一种很好的风格。引发Domain 异常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-17
  • 2012-01-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多