Question about callcc

Discussion in 'Ruby' started by Sam Kong, Oct 11, 2006.

  1. Sam Kong

    Sam Kong Guest

    Hi,

    I'm trying to understand continuation.
    There's some confusion in the following:

    In top-level,

    [1]
    callcc { |c| $label = c }
    #...some other code here
    $label.call

    [2]
    $label = callcc { |c| c }
    #...some other code here
    $label.call


    Are they same or different?
    As I understand it, callcc returns the last expression or return value
    of c.call.
    Thus, basically [1] and [2] should be same, right?
    But I'm not sure.
    Can anyone explain it, please?

    Thanks.

    Sam
    Sam Kong, Oct 11, 2006
    #1
    1. Advertising

  2. Sam Kong

    Carlos Guest

    Sam Kong wrote:

    > Hi,
    >
    > I'm trying to understand continuation.
    > There's some confusion in the following:
    >
    > In top-level,
    >
    > [1]
    > callcc { |c| $label = c }
    > #...some other code here
    > $label.call
    >
    > [2]
    > $label = callcc { |c| c }
    > #...some other code here
    > $label.call
    >
    >
    > Are they same or different?
    > As I understand it, callcc returns the last expression or return value
    > of c.call.
    > Thus, basically [1] and [2] should be same, right?
    > But I'm not sure.
    > Can anyone explain it, please?


    callcc returns the return value of its block, the first time. Later, it
    returns the value it was called with.
    If you test [2], you'll see the error message:
    undefined method `call' for nil:NilClass (NoMethodError)

    Try
    $label = callcc {|c| c}
    $label.call(3)

    and
    $label = callcc {|c| puts "first call"; c}
    $label.call(lambda{puts "second call"})

    to understand what's going on.
    --
    Carlos, Oct 11, 2006
    #2
    1. Advertising

  3. Sam Kong

    Ken Bloom Guest

    On Thu, 12 Oct 2006 01:59:26 +0900, Carlos wrote:

    > Sam Kong wrote:
    >
    >> Hi,
    >>
    >> I'm trying to understand continuation.
    >> There's some confusion in the following:
    >>
    >> In top-level,
    >>
    >> [1]
    >> callcc { |c| $label = c }
    >> #...some other code here
    >> $label.call
    >>
    >> [2]
    >> $label = callcc { |c| c }
    >> #...some other code here
    >> $label.call
    >>
    >>
    >> Are they same or different?
    >> As I understand it, callcc returns the last expression or return value
    >> of c.call.
    >> Thus, basically [1] and [2] should be same, right?
    >> But I'm not sure.
    >> Can anyone explain it, please?

    >
    > callcc returns the return value of its block, the first time. Later, it
    > returns the value it was called with.
    > If you test [2], you'll see the error message:
    > undefined method `call' for nil:NilClass (NoMethodError)
    >
    > Try
    > $label = callcc {|c| c}
    > $label.call(3)
    >
    > and
    > $label = callcc {|c| puts "first call"; c}
    > $label.call(lambda{puts "second call"})
    >
    > to understand what's going on.


    This example (run in straight ruby, not irb) should clarify what's going
    on.

    begin
    $label=nil
    $label=callcc{|c| c}
    puts "After the first callcc"
    $label.call
    rescue StandardError=>e
    puts e
    end

    begin
    $label=nil
    callcc{|c| $label=c}
    puts "After the second callcc"
    $label.call
    rescue StandardError=>e
    puts e
    end

    [bloom@cat-in-the-hat ~]$ ruby callcc.rb | head
    After the first callcc
    After the first callcc
    undefined method `call' for nil:NilClass
    After the second callcc
    After the second callcc
    After the second callcc
    After the second callcc
    After the second callcc
    After the second callcc
    After the second callcc
    (infintie loop)

    --
    Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
    Department of Computer Science. Illinois Institute of Technology.
    http://www.iit.edu/~kbloom1/
    I've added a signing subkey to my GPG key. Please update your keyring.
    Ken Bloom, Oct 11, 2006
    #3
  4. Sam Kong

    Sam Kong Guest

    Hi Ken,

    Ken Bloom wrote:
    > On Thu, 12 Oct 2006 01:59:26 +0900, Carlos wrote:
    >
    >>> callcc { |c| $label = c }
    >>> As I understand it, callcc returns the last expression or return value

    >> Try
    >> $label = callcc {|c| c}
    >> $label.call(3)
    >>
    >> and
    >> $label = callcc {|c| puts "first call"; c}
    >> $label.call(lambda{puts "second call"})
    >>
    >> to understand what's going on.

    >
    > This example (run in straight ruby, not irb) should clarify what's going
    > on.
    >
    > begin
    > $label=nil
    > $label=callcc{|c| c}
    > puts "After the first callcc"
    > $label.call
    > rescue StandardError=>e
    > puts e
    > end
    >
    > begin
    > $label=nil
    > callcc{|c| $label=c}
    > puts "After the second callcc"
    > $label.call
    > rescue StandardError=>e
    > puts e
    > end
    >
    > [bloom@cat-in-the-hat ~]$ ruby callcc.rb | head
    > After the first callcc
    > After the first callcc
    > undefined method `call' for nil:NilClass
    > After the second callcc
    > After the second callcc
    > After the second callcc
    > After the second callcc
    > After the second callcc
    > After the second callcc
    > After the second callcc
    > (infintie loop)


    Although I still don't understand what's going on in the back,
    I see the difference.

    Thank you very much.

    Sam

    --
    Posted via http://www.ruby-forum.com/.
    Sam Kong, Oct 12, 2006
    #4
    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. Jim Bob

    Why does Ruby have callcc?

    Jim Bob, Aug 6, 2003, in forum: Ruby
    Replies:
    22
    Views:
    240
  2. zuzu
    Replies:
    14
    Views:
    231
    Phil Tomson
    Aug 23, 2004
  3. Mikael Brockman
    Replies:
    2
    Views:
    131
    Mikael Brockman
    Sep 25, 2004
  4. Bill Atkins

    callcc Semantics

    Bill Atkins, Oct 18, 2004, in forum: Ruby
    Replies:
    2
    Views:
    100
    Eric Hodel
    Oct 19, 2004
  5. Greg Fitzgerald

    Callcc problem in Generator

    Greg Fitzgerald, Jan 25, 2005, in forum: Ruby
    Replies:
    0
    Views:
    179
    Greg Fitzgerald
    Jan 25, 2005
Loading...

Share This Page