Continuation - where does it continue

Discussion in 'Ruby' started by michelemendel@gmail.com, Sep 21, 2005.

  1. Guest

    Why does the following code print the line "doing other stuff" twice?


    def cc
    puts "do A"
    puts "----> 1. #{caller}"
    callcc { |cont| return cont}
    puts "----> 2. #{caller}"
    puts "do B"
    end

    puts "calling cc"
    cont = cc()
    puts "doing other stuff"
    cont.call() if cont
    puts "end of work"
    puts


    OUTPUT:
    calling cc
    do A
    ----> 1. remove2.rb:59
    doing other stuff
    ----> 2. remove2.rb:59
    do B
    doing other stuff
    end of work
     
    , Sep 21, 2005
    #1
    1. Advertising

  2. Responding to :

    > Why does the following code print the line "doing other stuff" twice?
    >
    > def cc
    > puts "do A"
    > puts "----> 1. #{caller}"
    > callcc { |cont| return cont}
    > puts "----> 2. #{caller}"
    > puts "do B"
    > end
    >
    > puts "calling cc"
    > cont = cc()
    > puts "doing other stuff"
    > cont.call() if cont
    > puts "end of work"
    > puts


    The continuation 'continues' from exactly the state where it left off:
    It starts after the callcc call, after returning from the cc method it
    continues one 'stack frame'[1] deeper after the cc() call, just as in
    normal execution - it won't 'jump back' to the code position after the
    continuation call.

    Best regards,
    Patrick

    [1] Java lingo, don't know what it's called in Ruby
     
    Patrick Roemer, Sep 21, 2005
    #2
    1. Advertising

  3. Pit Capitain Guest

    schrieb:
    > Why does the following code print the line "doing other stuff" twice?
    >=20
    > def cc
    > puts "do A"
    > puts "----> 1. #{caller}"
    > callcc { |cont| return cont}


    The continuation continues here, after the call to callcc.

    > puts "----> 2. #{caller}" =20
    > puts "do B"
    > end
    >=20
    > puts "calling cc"
    > cont =3D cc() # <------ (X)
    > puts "doing other stuff"
    > cont.call() if cont
    > puts "end of work"
    > puts
    >=20
    >=20
    > OUTPUT:
    > calling cc
    > do A
    > ----> 1. remove2.rb:59
    > doing other stuff
    > ----> 2. remove2.rb:59
    > do B
    > doing other stuff
    > end of work


    Above I've shown the place where the continuation continues. So when you=20
    call the continuation, you're back in the cc method after the callcc.=20
    The program prints the remaining messages of the cc method and returns=20
    nil (the result of puts). After the cc method returns, you're back at=20
    the place where the method has been called, which the line marked with=20
    (X). cont is set to nil, and the program prints "doing other stuff" for=20
    the second time.

    If you dont' want this, you have to change the code to something like

    puts "calling cc"
    cont =3D cc()
    if cont
    puts "doing other stuff"
    cont.call()
    end
    puts "end of work"
    puts

    Florian Gro=DF has written something to make Continuation handling easier=
    =20
    You should be able to find it in the mailing list archives.

    Regards,
    Pit
     
    Pit Capitain, Sep 21, 2005
    #3
  4. Jim Weirich Guest

    said:
    > Why does the following code print the line "doing other stuff" twice?
    >
    >

    01 def cc
    02 puts "do A"
    03 puts "----> 1. #{caller}"
    04 callcc { |cont| return cont}
    05 puts "----> 2. #{caller}"
    06 puts "do B"
    07 end
    >

    08 puts "calling cc"
    09 cont =3D cc()
    10 puts "doing other stuff"
    11 cont.call() if cont
    12 puts "end of work"
    13 puts

    This is the seqence of events (by line number). Comments on what's
    happening are in parenthesis.

    08: calling cc
    09 (calling cc)
    02: do A
    03: ----> 1. remove2.rb:59
    04: (returning the continuation)
    10: doing other stuff
    11: (calling the continuation, so picking up in the middle of cc)
    05: ----> 2. remove2.rb:59
    06: do B
    07: (returning from cc to the original location cc call site.
    Now we return a nil value)
    10: doing other stuff
    11: (since cc returned a nil the second time, we don't call the
    continuation this time)
    12: end of work

    --=20
    -- Jim Weirich http://onestepback.org
    -----------------------------------------------------------------
    "Beware of bugs in the above code; I have only proved it correct,
    not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)
     
    Jim Weirich, Sep 21, 2005
    #4
  5. michele Guest

    AHA - EUREKA!!!

    Thanks for your very quick answers! After "studying" continuations for
    a couple of days, reading everthing I could find about, I think I
    finally understand it. Your answers made it all clear.


    Michele
     
    michele, Sep 21, 2005
    #5
  6. Randy Kramer Guest

    On Wednesday 21 September 2005 10:36 am, Jim Weirich wrote:
    > said:
    > > Why does the following code print the line "doing other stuff" twice?


    > This is the seqence of events (by line number). Comments on what's
    > happening are in parenthesis.


    Thanks, the line numbers help a lot!

    regards,
    Randy Kramer
     
    Randy Kramer, Sep 21, 2005
    #6
  7. Hi,

    this finally cleared up Ruby's continuations for me too.

    Would this be the effect you're after?
    # code
    def cc
    puts "do A"
    puts "----> 1. #{caller}"
    cont =3D callcc { |cont| return cont}
    puts "----> 2. #{caller}"
    puts "do B"
    end

    puts "calling cc"
    if cont =3D cc()
    puts "doing other stuff"
    cont.call() if cont
    puts "end of work"
    puts
    end

    #output
    __END__
    calling cc
    do A
    ----> 1. continuations.rb:10
    doing other stuff
    ----> 2. continuations.rb:10
    do B
    #end

    I'm curious - what problem are you trying to solve?

    Regards,
    Sean



    On 9/21/05, michele <> wrote:
    > AHA - EUREKA!!!
    >
    > Thanks for your very quick answers! After "studying" continuations for
    > a couple of days, reading everthing I could find about, I think I
    > finally understand it. Your answers made it all clear.
    >
    >
    > Michele
    >
    >
    >
     
    Sean O'Halpin, Sep 22, 2005
    #7
  8. Guest

    What I don't understand is why the line:

    04 callcc { |cont| return cont}

    Doesn't give an 'undefined local method or variable "callcc"' error.
    To my noob eyes that looks like a method call w/an accompanying block.
    Have you got a clue for me?

    Thanks!

    -Roy
     
    , Sep 22, 2005
    #8
  9. Jim Weirich Guest

    On Wednesday 21 September 2005 10:51 pm, wrote:
    > What I don't understand is why the line:
    >
    > 04 callcc { |cont| return cont}
    >
    > Doesn't give an 'undefined local method or variable "callcc"' error.
    > To my noob eyes that looks like a method call w/an accompanying block.


    You've got good eyes. That's exactly what it is ... a call to the "callcc"
    method passing a block.

    --
    -- Jim Weirich http://onestepback.org
    -----------------------------------------------------------------
    "Beware of bugs in the above code; I have only proved it correct,
    not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)
     
    Jim Weirich, Sep 22, 2005
    #9
  10. michele Guest

    I justed wanted to understand how continuations work and where it
    happens, so I made the simplest example possible I could think of. I
    wanted to start work one place, leave it in the middle, do some other
    work, and then continue from where I left.

    Your answer as well as Pit Capitain's above solved the problem.
     
    michele, Sep 22, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. KathyB
    Replies:
    2
    Views:
    1,927
    Mario Vargas
    Jul 17, 2003
  2. DC

    Continuation question....

    DC, May 24, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    460
  3. Replies:
    3
    Views:
    429
  4. Karthik
    Replies:
    15
    Views:
    952
    Joachim Schmitz
    Jun 24, 2008
  5. Replies:
    2
    Views:
    84
Loading...

Share This Page