Search code examples
sortingsmllargenumberselection-sort

SML how to explicitly set a function parameter type to IntInf


I am trying to make a sorting function for unique large numbers in SML but the compiler keeps setting my function type to int instead of 'a. How can I explicitly tell the compiler to use IntInf?

Here is my code:

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

when I try

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));

it keeps giving me "Error: unbound type constructor: IntInf"


Solution

  • IntInf is the name of a module, the type is named IntInf.int. Alas, your code somewhat simplified:

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

    Note however that IntInf is an optional module that is not available on all implementations. (Also, you should add a case for the empty list.)