//
// Course: BU CAS CS 520, Fall 2010
// Instructor: Hongwei Xi (hwxi AT cs DOT bu DOT edu)
// Lecture on Thursday, Dec 2, 2010
//

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

(*
acker (0, n) = n + 1
acker (m+1, 0) = acker (m, 1)
acker (m+1, n+1) = acker (m, acker (m+1, n))
*)
fun acker
  (m: int, n: int): int =
  case+ m of
  | 0 => n+1
  | _ => (case+ n of
    | 0 => acker (m-1, 1)
    | _ => acker (m-1, acker (m, n-1))
    ) // end of [_]
// end of [acker]

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

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

fun kacker
  (m: int, n: int, k: cont(int)): ans =
  case+ m of
  | 0 => k(n+1)
  | _ => (case+ n of
    | 0 => kacker (m-1, 1, k)
    | _ => 
        kacker (m, n-1, lam res => kacker (m-1, res, k))
    ) // end of [_]
// end of [kacker]

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

implement
main () = () where {
  val ack33 = acker (3, 3)
  val () = (printf ("ack33 = %i\n", @(ack33)))
  val kack33 = kacker (3, 3, lam res => res)
  val () = (printf ("kack33 = %i\n", @(kack33)))
} // end of [main]

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

(* end of [kacker.dats] *)