Roger Costello
Roger Costello

Reputation: 3209

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

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?

Upvotes: 5

Views: 364

Answers (1)

Doug Currie
Doug Currie

Reputation: 41170

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)

Upvotes: 5

Related Questions