Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Well, I can't claim to great knowledge on this, but I'll give it a go. Schemers out there will probably laugh their heads off.

Essentially, a continuation is the abstract notion of "what do do next".

Languages like Scheme carry one of these around on each of their stack frames as something accessible from the language itself. call-with-current-continuation is simply a Higher Order Functionish way of doing this.

;; Guile scheme (call-with-current-continuation ;; Often abbr'd to call-cc. Takes a function as arg. (lambda (fred) (+ 3 4) (do-something 42 fred) (fred 42) ))

What's happening here is that when the outer call-cc is run, it takes the continuation of its current context -- where the call-cc would go next if it exited normally -- and calls (lambda (fred) ...) with the continuation in the formal parameter 'fred'.

When the function object in fred is called, the call-cc exits, and the interpreter goes merrily on its way. The call-cc call itself evaluates to whatever is passed to 'fred' when (or if) that gets called.

Note that the function in 'fred' can be passed around too, and called by other functions. The do-something function used above might decide to call its 'fred' argument with some answer other than 42.

You could fake Scheme's call-cc behaviour in Perl using an eval-BLOCK/if($@){} combo. The trick is distinguishing our fake death from a real thrown error:

sub call_cc (&) { my $clause = shift; my @fake_returns; my $fake_continuation; $fake_continuation = sub { @fake_returns = @_; die $fake_continuation; }; eval { return $clause->($fake_continuation); }; if ($@) { die $@ unless $@ == $fake_continuation; return @fake_returns; } #NOTREACHED } print STDERR call_cc { my $fred = shift; $fred->(42); return 64; # NOTREACHED }; print STDERR "\n";

But no, it's not a real continuation.

In reply to Re: (Perl6) Groking Continuations by andrewc
in thread (Perl6) Groking Continuations by crenz

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (3)
    As of 2018-01-20 11:48 GMT
    Find Nodes?
      Voting Booth?
      How did you see in the new year?

      Results (226 votes). Check out past polls.