//
// Course: BU CAS CS 520, Fall 2010
// Instructor: Hongwei Xi (hwxi AT cs DOT bu DOT edu)
// Lecture on Tuesday, Nov 30, 2010
//

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

typedef ans = int
typedef cont (a:t@ype) = a -<cloref1> ans

fun fact (x: int): int =
  if x > 0 then x * fact (x-1) else 1
// end of [fact]

fun kfact (x: int, k: cont (int)): ans =
  if x > 0 then
    kfact (x-1, lam res => k (x * res))
  else
    k (1)
  // end of [if]
// end of [kfact]

implement main () = () where {
  #define N 10
  val ans1 = fact (10)
  val () = printf ("fact(%i) = %i\n", @(N, ans1))
  val ans2 = kfact (10, lam res => res)
  val () = printf ("kfact(%i, ...) = %i\n", @(N, ans2))
} // end of [main]

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

(* end of [kfact.dats] *)