(*
** CAS CS525, Spring 2011
** Instructor: Hongwei Xi
*)

(* ****** ****** *)

staload "insort.sats"

(* ****** ****** *)

#define nil list0_nil
#define cons list0_cons
#define :: list0_cons

(* ****** ****** *)

implement insort (xs) = let
  fun ins (
    xs: list0 int, x0: int
  ) : list0 int =
     case xs of
     | cons (x, xs1) =>
         if x0 <= x then cons (x0, xs) else cons (x, ins (xs1, x0))
       // end of [cons]
     | nil () => cons (x0, nil ())
  // end of [ins]
in
  case+ xs of
  | cons (x, xs1) => ins (insort (xs1), x)
  | nil () => nil
end // end of [insort]

(* ****** ****** *)

(* end of [insort.dats] *)