[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Question about callcc

Sam Kong

10/11/2006 4:30:00 PM

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

4 Answers

Carlos

10/11/2006 4:59:00 PM

0

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.
--



Ken Bloom

10/11/2006 7:37:00 PM

0

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...
I've added a signing subkey to my GPG key. Please update your keyring.

Sam Kong

10/12/2006 12:08:00 AM

0

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-....

Edster

10/28/2013 10:21:00 AM

0

JNugent <jenningsltd@fastmail.fm> wrote:


>On 25/10/2013 19:25, Cynic wrote:
>
>> On 25/10/2013 18:53, Mentalguy2k8 wrote:
>>
>>> So he's on the equivalent of ?1.25 an hour. Do you foresee a stampede to
>>> the Job Centres?
>>
>> There won't *be* any job centers, that's one of the main points (and a
>> big saving of money).
>>
>> And yes, people will work for ?1.25 per hour if it makes them
>> significantly better off - which a 25% increase in total income
>> certainly will.
>>
>> The problem with our present benefits system is that working does *not*
>> necessarily make the benefits claimant any better off at all...
>
>...so benefits need to be set some way below what the claimant can
>command in the job market.
>
>Benefits were never meant to be an option.

The more logical solution would be to increase minimum wage and/or
in-work benefits so people were always better off working than not
working.