From: Gerald Smith
Subject: Scheme to CL??
Date: 
Message-ID: <5l76qv$rlt$1@mhafn.production.compuserve.com>
Does anyone have experience using the continuation-passing 
macros from Paul Graham's book 'On Lisp' ??


Really thought I cracked the problem, because of my minor success 
in converting this Scheme code into it's CL counterpart:

;; Scheme code
;;***************************************************
;; Here is a simple 'append' form:

(append
   '(low-level append returned this)
   '(erroneous result))


;; Scheme code of the above simple 'append' form, rigged so
;; we can simulate 'breaking-into' it, using continuations, of course:

(define call/cc  call-with-current-continuation)

(define frozen '() )

(append
   '(low-level append returned this)
   (call/cc
      (lambda (cc)
         (set! frozen cc)
        '(erroneous result))))

;; returns ===> (low-level append returned this erroneous result)



(frozen '(correct result))

;; returns ===> (low-level append returned this correct result)

;;*************************************************







;; Below is the CL version, runs OK
;;**************************************************
;; We can do the same thing using Common Lisp and Paul Graham's macros,
;; but our source-code will be more bulky :

(defvar frozen '() )

(progn
  (setq frozen   (=lambda (x)
                  (declare (ignore *cont*))
                     (append '(low-level append returned this)
                              x) ))
  (=funcall frozen '(erroneous message)))

;; returns ===> (LOW-LEVEL APPEND RETURNED THIS ERRONEOUS RESULT)




;; "breaking into" the middle of the 'append' form, with a minor change:

(=funcall frozen '(correct result))

;; returns ===> (LOW-LEVEL APPEND RETURNED THIS CORRECT RESULT)
;;**************************************************







Here is the Scheme code I failed to convert.  This code is a simple loop, equivilent to Common Lisp's DOTIMES, but no iteration or recursion is used.  It demonstrates that continuations can be used to implement looping.


(define first  (lambda (n) (car n)))

(define second  (lambda (n) (cadr n)))

(define call/cc  call-with-current-continuation)

(define print  (lambda (arg) (display arg) (newline)))

(define make-receiver
  (lambda (n)
    (let  ((receiver  (lambda (proc) (list n proc) )))
      (call/cc receiver) )))

(define countdown
  (lambda (n)
    (let ((pair (make-receiver n)))
      (let ( (v (first pair))
             (returner (second pair)))
        (print v)
        (if (positive? v)
            (returner (list (sub1 v) returner))
            (print "Blastoff"))) )))


Running countdown:

(countdown 4)

4
3
2
1
0
Blastoff



I have read and re-read all my material on continuations, to no avail.

Wish I had the "On Lisp" author's email address (Paul Graham) - - he might be kind enough to supply me with a CL conversion of the above code - - that would break the gridlock that I find myself in.

Gerald -