callcc

Execute the following call/cc expression

孤人 提交于 2020-01-14 04:34:27
问题 I use racket and I got the result 4 for following simple code: (let/cc done ((let/cc esc (done (+ 1 (let/cc k (esc k))))) 3)) and I was going to execute this code step-by-step. First, I changed the first let/cc into the form of call/cc like below: (call/cc (λ (done) ((let/cc esc (done (+ 1 (let/cc k (esc k))))) 3))) Of course, this produces 4 also. Second, since I found the mechanism of call/cc in the internet which says call/cc do following 4 steps: Captures the current continuation.

How to use continuations in scheme?

淺唱寂寞╮ 提交于 2020-01-05 04:56:10
问题 I'm trying to understand call/cc operator in Scheme. I'm planing of implementing this in my JavaScript lisp. This is my simple code: (letrec ((x 0) (f (lambda (r) (set! x r) (display (r 20)) (display "10")))) (display (call/cc f)) (x "30")) I tough that it should print 20 then 30 then 10. But it create infinite loop (it keep printing 30). How this code should look like to display 3 values, call display 3 times? Should it be possible to create loops that don't consume stack with continuations?

Seasoned Schemer's get-first, get-next, and waddle functions

你。 提交于 2020-01-02 05:06:05
问题 (define get-first (lambda (l) (call-with-current-continuation (lambda (here) (set! leave here) (waddle l) (leave (quote ())))))) (define get-first (lambda (l) (call-with-current-continuation (lambda (here) (set! leave here) (leave (waddle l)))))) For anybody not familiar with the book "The Seasoned Schemer", get-first , get-next , and waddle (last two not defined here) are procedures to apparently model coroutines to iterate through a tree passed to waddle that yields leaves only. Just prior

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

Clarification on callCC

风格不统一 提交于 2019-12-25 01:14:11
问题 My background is Javascript, Python & a bit of Haskell. Hi I am new to Scheme (1 day old). I want to understand the difference between below 2 code snippets. (define onePlus (lambda (v) (+ 1 v))) (onePlus 4) ; 5 With CallCC (define anotherOnePlus 0) (+ 1 (call/cc (lambda (k) (set! anotherOnePlus k) (k 4)))) ; 5 (anotherOnePlus 4); 5 Why anyone want to do the 2nd way to get hold of the function you live in. What am I missing in a bigger picture? Is there any limitations of Scope for getting

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

The semantic of call/cc and “ensure” in Ruby

醉酒当歌 提交于 2019-12-13 15:41:22
问题 As I know so far, Ruby is the only mainstream language that supports both call/cc and try/catch/finally (written as begin/rescue/ensure/end block). I am not familiar with Ruby, but my intuitive tell me that there are potential conflicts of that two, since call/cc allows arbitrarily control flow and ensure require some guaranteed control flow (some code path MUST be executed in a pre-defined situation, namely leaving the containing block). So, are there any conflicts exists in the language? if

call/CC with closures

这一生的挚爱 提交于 2019-12-10 15:27:34
问题 Wikipedia mentions that "In any language which supports closures and proper tail calls, it is possible to write programs in continuation-passing style and manually implement call/cc." How would one implement this function in for example in javascript? I know that javascript doesn't do tco, but assuming that stack space doesn't run out 回答1: It is not possible to write a call/cc implementation in JavaScript: JavaScript does not meet the requirement of "proper tail calls" (no additional stack

Understanding Haskell callCC examples

大兔子大兔子 提交于 2019-12-06 03:33:12
问题 I am having trouble understanding the answers to a previous question. I'm hoping that an explanation of the following will clarify things. The following example comes from fpcomplete import Control.Monad.Trans.Class import Control.Monad.Trans.Cont main = flip runContT return $ do lift $ putStrLn "alpha" (k, num) <- callCC $ \k -> let f x = k (f, x) in return (f, 0) lift $ putStrLn "beta" lift $ putStrLn "gamma" if num < 5 then k (num + 1) >> return () else lift $ print num The output is alpha

Understanding Haskell callCC examples

帅比萌擦擦* 提交于 2019-12-04 08:24:08
I am having trouble understanding the answers to a previous question . I'm hoping that an explanation of the following will clarify things. The following example comes from fpcomplete import Control.Monad.Trans.Class import Control.Monad.Trans.Cont main = flip runContT return $ do lift $ putStrLn "alpha" (k, num) <- callCC $ \k -> let f x = k (f, x) in return (f, 0) lift $ putStrLn "beta" lift $ putStrLn "gamma" if num < 5 then k (num + 1) >> return () else lift $ print num The output is alpha beta gamma beta gamma beta gamma beta gamma beta gamma beta gamma 5 I think I understand how this