From: Luke Graham Date: 2005-02-14T18:00:29+09:00 Subject: Re: can anyone verify this code as correct? On Mon, 14 Feb 2005 17:52:06 +0900, E S wrote: > > L�hett�j�: Luke Graham > > Aihe: Re: can anyone verify this code as correct? > > > > On Mon, 14 Feb 2005 17:07:00 +0900, E S wrote: > > > > L�hett�j�: Luke Graham > > > > Aihe: can anyone verify this code as correct? > > > > > > > > class Object > > > > def choices(choicelist) > > > > if (!Class.class_variables.include? "@@cont") > > > > @@cont = [] > > > > end > > > > > > You can use '@@cont ||= []'. > > > > Thanks, I like that one. > > > > > > choicelist.each { |choice| > > > > callcc { |cc| > > > > @@cont << cc > > > > return choice > > > > } > > > > } > > > > nil > > > > end > > > > > > Continuation returns after the block, either with the value of the > > > block or the parameter to #call, and it stores its execution context. > > > Here you're returning in the middle of a loop, so I'm thinking each > > > #call will cause the remaining 'choicelist' to be iterated over again, > > > so the actual return value is going to be 'choicelist'. > > > > I dont understand... the return value is definitely not choicelist, but > > a different choice each time. The callcc is -inside- the internal iteration. > > After all values have been exhausted, nil is the last thing to be > > returned, and effectively marks the limit of choices, as '\0' does for > > c-strings. The array could probably stand to be flattened first, just > > in case. > > Ah, indeed. That's what I get for omitting 'return' (all keystrokes > count at 03:00 :) So yes, the code remains valid (just without the > interesting side-effects). They certainly do count, even at 3am when our human brains can no longer compete with silicon. :) Would you consider this a non-evil use of continuations? If so, I will be very pleased with myself for re-inventing it. Havent seen it before personally, but nothing is new in this world - Im sure LISP had it years ago ;)