Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8822169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:02:28+00:00 2026-06-14T06:02:28+00:00

I am looking at the following example for a coroutine from http://community.schemewiki.org/?call-with-current-continuation : (define

  • 0

I am looking at the following example for a coroutine from http://community.schemewiki.org/?call-with-current-continuation:

 (define (hefty-computation do-other-stuff) 
    (let loop ((n 5)) 
      (display "Hefty computation: ") 
      (display n) 
      (newline) 
      (set! do-other-stuff (call/cc do-other-stuff)) ; point A
      (display "Hefty computation (b)")  
      (newline) 
      (set! do-other-stuff (call/cc do-other-stuff)) 
      (display "Hefty computation (c)") 
      (newline) 
      (set! do-other-stuff (call/cc do-other-stuff)) 
      (if (> n 0) 
          (loop (- n 1))))) 

superfluous work:

 ;; notionally displays a clock 
 (define (superfluous-computation do-other-stuff) 
    (let loop () 
      (for-each (lambda (graphic) 
                  (display graphic) 
                  (newline) 
                  (set! do-other-stuff (call/cc do-other-stuff))) 
                '("Straight up." "Quarter after." "Half past."  "Quarter til.")) ; point B
      (loop))) 


(hefty-computation superfluous-computation) 

For the first usage of call/cc, what is the context supposed to be? When I say context, I mean where are we supposed to “return to” as a result of callcc’s jump?

From what I understand, the first time you call call/cc, do-other-stuff essentially becomes a procedure that executes the code of superfluous-computation and then jumps to the point right after the set! (point A). The second time, it will wrap its “jump to point B” behavior around the “jump to point A and execute the context, or whatever code follows point A”. Is this correct?

It doesn’t seem like this code would work if the set! actually happened. Or is the set! necessary for this code to work?

A visual representation of what’s going on would really help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:02:30+00:00Added an answer on June 14, 2026 at 6:02 am

    The context of call/cc is where ever call/cc is being called from. You can almost think of a call/cc like goto that jumps the code right back to where you were before and substitutes (call/cc whatever) with the return value.

    call/cc basically says, "let’s go do this function and give it away to jump right back here and forget about whatever else it was doing"

    Ok when I was trying to understand call/cc for the first time, I found this code confusing in the extreme so let’s look at a simplified coroutine example:

    (define r1
      (lambda (cont2)
        (display "I'm in r1!")
        (newline)
        (r1 (call/cc cont2      ))))
        ;  ^---------------cont1
    
    (define r2
      (lambda (cont1)
        (display "I'm in r2!")
        (newline)
        (r2 (call/cc cont1      ))))
        ;  ^---------------cont2
    

    Ok this is exactly the same concept as your code. But it’s much simpler.

    In this case, if we call (r1 r2) this prints

    I'm in r1
    I'm in r2
    I'm in r1
    I'm in r2    
    I'm in r1
    I'm in r2    
    I'm in r1
    I'm in r2
    ...
    

    Why? Because r1 first takes in r2 as cont2 so it announces to us that it’s in r1. And then it recurses on itself with the result of (call/cc cont2) aka (call/cc r2).

    Ok so what’s the return of this? well (call/cc r2) calls r2 with the current continuation as cont1 so will announce that it’s in r2 and then recurse on itself with the result of (call/cc cont1). Ok so what was cont1 again? cont1 was a continuation to that expression before in r1. So when we call it here, we pass back a continuation to the spot we’re currently at. Then we forget anything about what we were doing in r2 and hop back into executing r1.

    This repeats in r1 now. We announce stuff and then jump back to where we where before in r2 with and our expression from before, (call/cc cont1) returns a continuation to where we were in r1 and then we continue in our merry infinite loop.

    Back to your code

    In your code the concept is exactly the same. In fact `superfluous-computation` is almost identical to the above functions when you stop and think about it. So what’s up with the `set!`s? In this code all they do is change the value of `do-other-work` to the newest continuation. That’s it. In my example I used recursion. In this example they use `set!`.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking at the following API: http://wiki.github.com/soundcloud/api/oembed-api The example they give is Call: http://soundcloud.com/oembed?url=http%3A//soundcloud.com/forss/flickermood&format=json
Looking at the following example of an embedded Jetty Example: http://musingsofaprogrammingaddict.blogspot.com.au/2009/12/running-jsf-2-on-embedded-jetty.html The following code
From another forum I found the following example: I was looking for a way
Hi I was looking at the following Fragments example on the android site. http://developer.android.com/guide/components/fragments.html#Example
Trying to customize Symfony2 form to produce html code looking like the following example:
I am looking for a good example of how to achieve the following: I
I come from ruby/C# and am new to Python. I'm looking at the following
In the following example, is there a way to know a merge happened? Looking
In the following example c code, used in an Arduino project, I am looking
The following example is from the book 'Programming in Scala'. Given a class 'Rational'

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.