From: Gerald Smith
Subject: Specialized help requested
Date: 
Message-ID: <5m38qa$f5v$1@mhafn.production.compuserve.com>
Does anyone out there have experience using Paul Graham's macros
from chapter 20 of the book titled "On Lisp"

Those six macros allow *most* of the functionality of Scheme
"continuations" to be ported to Common Lisp.

I want to do opportunistic backtracking in CL, without going through
all the hassle that would occur if I did not have access to continuations.

Speed of execution is not a consideration, because I plan to convert
all the continuations to their faster running counterparts, once the
continuation version is up and running.

I feel if some kind soul will show me how to use Paul's macros
to convert the Scheme code below to its CL version, I feel that
I can then get by on my own, on subsequent conversions.

The code below is similar to CL's 'dotimes' loop - - I have no
interest in it other than learning how to use the macros in order
to convert it to CL.

For the life of me, I can't convert it, no matter how much I re-read
the books.

; Scheme code - - load into MacGambit or MacScheme
;*******************************************

(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 attempt
  (lambda (n)
    (print "Likewise this message")
    (let  ((receiver  (lambda (proc) (list n proc) )))
      (call/cc receiver) )))

(define countdown
  (lambda (n)
    (newline)
    (print "This message only appears once")
    (let ((pair (attempt n)))
      (let ( (v (first pair))
             (returner (second pair)))
        (print v)
        (if (positive? v)
            (returner (list (- v 1) returner))
            (print "Blastoff"))
        (newline)
        (newline)) )))

;*******************************************
; End of code


Running countdown on MacGambit Scheme (free) produces this printout

: (countdown 3)

This message only appears once
Likewise this message
3
2
1
0
Blastoff


#f

: 


The colon character is the prompt in the MacGambit version of Scheme.

The last  #f  is just the returned value of the program.

The first four functions are not really necessary, they just improve the readability of the program for those of us who are used to Common Lisp.


Gerald
From: Rolf-Thomas Happe
Subject: Re: Specialized help requested
Date: 
Message-ID: <r5pvue7f83.fsf@xtreme.mathematik.uni-freiburg.de>
In article <············@mhafn.production.compuserve.com> Gerald Smith
 <··········@CompuServe.COM> writes:

[...]
   I feel if some kind soul will show me how to use Paul's macros
   to convert the Scheme code below to its CL version, I feel that
   I can then get by on my own, on subsequent conversions.
[...]

Here's what I came up with:

;;; From On_Lisp / begin
;;; (the book code is available online, forgot where)

(setq *cont* #'identity)

(defmacro =lambda (parms &body body) 
  `#'(lambda (*cont* ,@parms) ,@body))

(defmacro =defun (name parms &body body)
  (let ((f (intern (concatenate 'string
                                "=" (symbol-name name)))))
    `(progn
       (defmacro ,name ,parms
         `(,',f *cont* ,,@parms))
       (defun ,f (*cont* ,@parms) ,@body))))
 
(defmacro =bind (parms expr &body body)
  `(let ((*cont* #'(lambda ,parms ,@body))) ,expr))
    
(defmacro =values (&rest retvals)
  `(funcall *cont* ,@retvals))

(defmacro =funcall (fn &rest args)
  `(funcall ,fn *cont* ,@args))

(defmacro =apply (fn &rest args)
  `(apply ,fn *cont* ,@args))

;;; From On_Lisp / end

(=defun attempt (n)
   (print "Likewise this message")
   (=values n (=funcall #'(lambda (cc) cc))))

(=defun countdown (n)
  (terpri)
  (print "This message only appears once")
  (=bind (n k) (attempt n)
    (print n)
    (if (plusp n)
	(funcall k (- n 1) k)
        (format t "~%Blast off~%~%"))))

;;; end of code

Perhaps somewhat dirty, but it should be correct (tested in CLISP).

rthappe