//
// Course: BU CAS CS 520
// Instructor: Hongwei Xi (hwxi AT cs DOT bu DOT edu)
//

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

#define nil list0_nil
#define cons list0_cons

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

extern fun genlst (l: int, u: int): list0 int

implement genlst (l, u) =
  if l < u then cons (l, genlst (l+1, u)) else nil ()
// end of [genlst]

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

extern fun length (xs: list0 int): int

implement length (xs) = case+ xs of
  | cons (_, xs) => 1 + length (xs) | nil _ => 0
// end of [length]

val () = let // code for testing
  val N = 123 in assert (length (genlst (0, N)) = N)
end // end of [let]

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

extern fun multiply (xs: list0 int): int

implement multiply (xs) = case+ xs of
  | cons (x, xs) => x * multiply (xs) | nil _ => 1
// end of [multiply]

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

extern fun fact (n: int): int

implement fact (n) = multiply (genlst (1, n+1))

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

implement main () = let
  val n = 10
  val ans = fact (n)
  val () = printf ("fact (%i) = %i\n", @(n, ans));
in
  // nothing
end // end of [main]

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

(* end of [list.dats] *)