[comp.lang.scheme] call-with-current-continuations and leaf compare

jaffer@gerber.ai.mit.edu (Aubrey Jaffer) (02/02/91)

;;; The function leaf-eq? attempts to compare the leaves of 2
;;; arbitrary trees constructed of lists.  Unfortunately, it doesn't
;;; work right.  It compares the leaves correctly until one of the
;;; trees run out.  That next-leaf-generator then starts out at
;;; the beginning of the tree again.  This doesn't happen when a
;;; single leaf-generator is made and repeatedly called.  The
;;; following code has write statements in it to illustrate each
;;; comparison. WHAT IS WRONG HERE?

(define (next-leaf-generator obj eot)
  (letrec ((return #f)
	   (cont (lambda (x) (recurse obj) (set! cont eot) (eot #t)))
	   (recurse (lambda (obj)
		      (if (pair? obj)
			  (for-each recurse obj)
			  (call-with-current-continuation
			   (lambda (c)
			     (set! cont c)
			     (return obj)))))))
    (lambda () (call-with-current-continuation
		(lambda (ret) (set! return ret) (cont #f))))))

(define (leaf-eq? x y)
  (let* ((eot (list 4))
	 (eotf (lambda (x) eot))
	 (xf (next-leaf-generator x eotf))
	 (yf (next-leaf-generator y eotf)))
	(letrec ((loop (lambda (x y)
			(write x) (display " ") (write y) (newline)
			(cond ((not (eq? x y)) #f)
			      ((eq? eot x) #t)
			      (else (loop (xf) (yf)))))))
		(loop (xf) (yf)))))

(write (leaf-eq? '(a b c) '((a) b c)))

jar@altdorf.ai.mit.EDU (Jonathan A Rees) (02/04/91)

   Date: 2 Feb 91 06:21:14 GMT
   From: Aubrey Jaffer <jaffer@gerber.ai.mit.edu>

   ... WHAT IS WRONG HERE?

   (define (next-leaf-generator ...)
     (letrec (... (recurse ...)) ...))

What is wrong here is that "recurse" is not a word.  (Check any
English language dictionary.)  I think you mean "recur," or perhaps
"recourse."  Derivatives of the verb "to recur" include "recurrent"
and "recursion."  If "to recurse" were to mean anything at all, it
would mean "to curse again," which, now that I think of it, might be
what you are inclined to do at this point.