Question about callcc

S

Sam Kong

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
 
C

Carlos

Sam said:
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.
--
 
K

Ken Bloom

Sam said:
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)
 
S

Sam Kong

Hi Ken,

Ken said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top