Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a Scheme call-with-current-continuation to Common Lisp?

Tags:

scheme

I am converting some Scheme code to Common Lisp. I don't know Scheme. I know a bit of Common Lisp.

Here is the Scheme code:

(define (with-process-abortion thunk)
    (call-with-current-continuation
        (lambda (k)
            (fluid-let ((*abort-process* k))
                (thunk)))))

I did some reading on the Scheme call-with-current-continuation function but, honestly, I have no idea what the above function is doing. My conversion to Common Lisp is very skeletal at this time:

(defun with-process-abortion (thunk)
    ;; No idea how to implement
    )

This SO post says:

every occurrence of call/cc can be replaced with the following equivalent:

(lambda (f k) (f (lambda (v k0) (k v)) k))

where k is the continuation to be saved, and (lambda (v k0) (k v)) is the escape procedure that restores this continuation (whatever continuation k0 that is active when it is called, is discarded).

Okay, what would f correspond to in my situation? What would k correspond to?

like image 315
Roger Costello Avatar asked May 02 '16 20:05

Roger Costello


1 Answers

You cannot solve this problem in general because Common Lisp does not have call/cc or anything that implements "full continuations." However, you can probably convert this code because it appears that the Scheme implementation is using call/cc only for non-local exits, and Common Lisp supports this with catch and throw, as well as with restarts.

You could try replacing a uses of (with-process-abortion thunk) with

`(catch 'wpa #,thunk)

and replace (*abort-process*) with (throw 'wpa nil)

like image 82
Doug Currie Avatar answered Nov 14 '22 22:11

Doug Currie