2014-12-07 62 views
0

我想为SML中的唯一大数制作排序函数,但编译器一直将我的函数类型设置为int而不是'a。我怎样才能明确地告诉编译器使用IntInf?SML如何显式设置函数参数类型为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”

+0

使用变通方法求解:'if(hd(choicesort(t))+ IntInf.fromInt(0)) 2014-12-07 16:08:49

回答

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是一个可选的模块,它并不适用于所有的实现。 (另外,您应该为空列表添加一个案例。)

+0

我做了一个非常具体的代码段,所以空列表将不会被排序。 – 2014-12-08 10:15:51

+0

@ Jean-LucNacifCoelho,无论如何添加遗失的情况都被认为是很好的风格,引发一个'Domain'异常。 – 2014-12-08 14:10:40