continuations

Why continuation passing style

别说谁变了你拦得住时间么 提交于 2019-12-31 08:58:12
问题 In The Scheme Programming Language by Kent Dybvig (4th edition) section 3.4, he describes very clearly what continuation passing style is. For the why he gives two reasons: pass more than one result to its continuation, because the procedure that implements the continuation can take any number of arguments. CPS also allows a procedure to take separate continuations ..., which may accept different numbers of arguments. Since the first reason can also be done using the values procedure and the

Why continuation passing style

喜你入骨 提交于 2019-12-31 08:58:05
问题 In The Scheme Programming Language by Kent Dybvig (4th edition) section 3.4, he describes very clearly what continuation passing style is. For the why he gives two reasons: pass more than one result to its continuation, because the procedure that implements the continuation can take any number of arguments. CPS also allows a procedure to take separate continuations ..., which may accept different numbers of arguments. Since the first reason can also be done using the values procedure and the

Can call-with-current-continuation be implemented only with lambdas and closures?

我们两清 提交于 2019-12-30 00:32:22
问题 Does anyone know if call/cc can be implemented with just lambdas and closures? It seems that call/cc interrupts the program's flow (like an exception) but lambdas and closures can't do that. Therefore I think call/cc can't be implemented via lambdas and closures. Any more ideas? 回答1: The question is not particularly clear, since what exactly does "implemented with just lambdas and closures" mean? In any case, continuations can be used in any language with closures by manually writing in

Explaining different behavior of variables referenced in continuations?

冷暖自知 提交于 2019-12-25 03:06:22
问题 Following are two case which have different behaviors regarding the value of i across calls to stored continuations. How can the difference be explained? Case A >(define cc #f) >(define (x) (let ((i 0)) (set! i (+ i 100)) (+ i (+ i (call/cc (lambda (k) (set! cc k) 1)))) ; call/cc is not included by set! (set! i (+ i 10)) i)) > (x) 110 > (cc 50) ; the context variable i changes as cc calling 120 > (cc 50) 130 Case B > (define cc #f) > (define (x) (let ((i 0)) (set! i (+ i 100)) (set! i (+ i

interpret Parigot's lambda-mu calculus in Haskell

☆樱花仙子☆ 提交于 2019-12-21 01:06:13
问题 One can interpret the lambda calculus in Haskell: data Expr = Var String | Lam String Expr | App Expr Expr data Value a = V a | F (Value a -> Value a) interpret :: [(String, Value a)] -> Expr -> Value a interpret env (Var x) = case lookup x env of Nothing -> error "undefined variable" Just v -> v interpret env (Lam x e) = F (\v -> interpret ((x, v):env) e) interpret env (App e1 e2) = case interpret env e1 of V _ -> error "not a function" F f -> f (interpret env e2) How could the above

call/cc in Lua - Possible?

橙三吉。 提交于 2019-12-20 09:31:04
问题 The Wikipedia article on Continuation says: "In any language which supports closures , it is possible to write programs in continuation passing style and manually implement call/cc ." Either that is true and I need to know how to do it or it is not true and that statement needs to be corrected. If this is true, please show me how to implement call/cc in Lua because I can't see how. I think I'd be able to implement call/cc manually if Lua had the coroutine.clone function as explained here. If

boost::future and continuations - value set, but future still blocks

淺唱寂寞╮ 提交于 2019-12-20 03:34:08
问题 I am trying to make the following continuation work - but f.get() blocks. Whats wrong? #include <iostream> #define BOOST_THREAD_PROVIDES_FUTURE #define BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION #include <boost/thread/future.hpp> struct Foo { boost::future<int> start() { return p.get_future(); } void finish() { p.set_value(23); } boost::promise<int> p; }; int main () { Foo foo; foo.start().then([](boost::future<int> f) { std::cout << "done:" << std::endl; std::cout << f.get() << std::endl; });

How and why does the Haskell Cont monad work?

时间秒杀一切 提交于 2019-12-17 17:23:18
问题 This is how the Cont monad is defined: newtype Cont r a = Cont { runCont :: (a -> r) -> r } instance Monad (Cont r) where return a = Cont ($ a) m >>= k = Cont $ \c -> runCont m $ \a -> runCont (k a) c Could you explain how and why this works? What is it doing? 回答1: The first thing to realize about the continuation monad is that, fundamentally, it's not really doing anything at all. It's true! The basic idea of a continuation in general is that it represents the rest of a computation . Say we

Implement yield and send in Scheme

痞子三分冷 提交于 2019-12-17 16:23:12
问题 I'm trying to port yield and yield from from Python to Scheme. Here is an implementation I've done: (define (coroutine routine) (let ((current routine) (status 'new)) (lambda* (#:optional value) (let ((continuation-and-value (call/cc (lambda (return) (let ((returner (lambda (value) (call/cc (lambda (next) (return (cons next value))))))) (if (equal? status 'new) (begin (set! status 'running) (current returner)) (current (cons value returner))) (set! status 'dead)))))) (if (pair? continuation

Continuations in Java

谁都会走 提交于 2019-12-17 15:39:14
问题 Is there a good implementation of continuations in Java? If so, what is the overhead like? The JVM wasn't designed with these sort of things in mind, right? So is this kind of going against the grain? 回答1: See Apache Javaflow http://commons.apache.org/sandbox/javaflow/ It's the only continuation package for java that's actively under development. The other one, RIFE, I'm not sure which state it's in. 回答2: Javaflow http://commons.apache.org/sandbox/javaflow/ Play framework use Javaflow http:/