//
// 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 fib (x: int): int =
  if x >= 2 then fib (x-1) + fib (x-2) else x
// end of [fib]

fun kfib (x: int, k: cont(int)): ans =
  if x >= 2 then
    kfib (x-1, lam res1 => kfib (x-2, lam res2 => k(res1 + res2)))
  else k (x)
// end of [kfib]

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

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

(* end of [kfib.dats] *)