;--- file 4-reverse-k-data-structure.ss -------------------
;--- implement the continuations ADT as data-structures ---

(load "chez-init.ss")

(define any? (lambda (x) #t))

(define-datatype continuation continuation?
  [init-k]
  [append-k (a any?) (k continuation?)]
  [rev1-k (car-L any?) (k continuation?)]
  [rev2-k (reversed-cdr (list-of any?)) (k continuation?)])

(define apply-k
  (lambda (k v)
    (cases continuation k
      [init-k () (printf "answer: ~s~n" v)]
      [append-k (a k) (apply-k k (cons (car a) v))]
      [rev1-k (car-L k)
	(if (pair? car-L)
	    (reverse*-cps car-L (rev2-k v k))
	    (append-cps v (list car-L) k))]
      [rev2-k (reversed-cdr k)
	(append-cps reversed-cdr (list v) k)])))

(define reverse*-cps
  (lambda (L k)
    (if (null? L)
        (apply-k k '())
        (reverse*-cps (cdr L)
                      (rev1-k (car L) k)))))
(define append-cps
  (lambda (a b k)
    (if (null? a)
        (apply-k k b)
        (append-cps (cdr a)
                    b
                    (append-k a k)))))
(define test
  (lambda () 
   (reverse*-cps '(a ((b c) () ((d))))
                 (init-k))))
(define trace-all
  (lambda ()(trace reverse*-cps append-cps apply-k)))