[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Delimited continuations
From: |
Michael Heerdegen |
Subject: |
Re: Delimited continuations |
Date: |
Sun, 10 Dec 2017 17:53:00 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) |
"John Wiegley" <address@hidden> writes:
> ;; -*- lexical-binding: t -*-
>
> (defun shift (k entry)
> (if (eq (nth 0 k) 'outer)
> (throw (nth 1 k)
> (funcall entry #'(lambda (val)
> (funcall (nth 2 k)
> (list 'inner val)))))
> (nth 1 k)))
>
> (defun reset (thunk)
> (let ((bound (make-symbol "reset--bound")))
> (catch bound
> (funcall thunk (list 'outer bound thunk)))))
This seems to be clever.
But one little disadvantage seems to be that a part of the code (the
part of the `reset' thunk that is evaluated until `shift' is reached) is
processed twice: once till the `shift' is reached (which `throw's and
hasn't access to this calculation, so that part of the calculation is
thrown away) and a second time when the delimited continuation is
called.
E.g. in
#+begin_src emacs-lisp
(reset (lambda (p)
(+ (+ 1 2)
(shift p (lambda (k) (funcall k 1))))))
#+end_src
(+ 1 2) is executed twice. I think this isn't necessarily so?
BTW, I wonder what one can potentially do with this stuff.
Michael.