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

(* ****** ****** *)
//
// HX: this one is not tail-recursive
//
fun fact (n: int): int =
  if n > 0 then n * fact (n-1) else 1
// end of [fact]

(* ****** ****** *)
//
// HX: this one is tail-recursive
//
fun fact2 (n: int, res: int): int =
  if n > 0 then fact2 (n-1, n * res) else res
// end of [fact2]

(* ****** ****** *)
//
// HX: an example of mutual recursion
//
fn* isevn (n: int): bool =
  if n = 0 then true else isodd (n-1)
and isodd (n: int): bool =
  if n = 0 then false else isevn (n-1)
  
(* ****** ****** *)

implement
main () = () where {
  val () = println! ("fact (10) = ", fact (10))
  val () = println! ("fact (33) = ", fact (33))
  val () = println! ("fact (34) = ", fact (34))
//
  val () = println! ("isevn (~1) = ", isevn (~1))
  val () = println! ("isodd (~1) = ", isodd (~1))
//
} // end of [main]

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

(* end of [misc.dats] *)