Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Scheme (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===First-class continuations=== {{Main|Continuation}} Continuations in Scheme are [[first-class object]]s. Scheme provides the procedure <code>[[call-with-current-continuation]]</code> (also known as <code>call/cc</code>) to capture the current continuation by packing it up as an escape procedure bound to a formal argument in a procedure provided by the programmer. (R5RS sec. 6.4)<ref name="r5rs"/> First-class continuations enable the programmer to create non-local [[Control flow|control constructs]] such as [[iterator]]s, [[coroutine]]s, and [[backtracking]]. Continuations can be used to emulate the behavior of [[return statement]]s in imperative programming languages. The following function <code>find-first</code>, given function <code>func</code> and list <code>lst</code>, returns the first element <code>x</code> in <code>lst</code> such that <code>(func x)</code> returns true. <syntaxhighlight lang="scheme"> (define (find-first func lst) (call-with-current-continuation (lambda (return-immediately) (for-each (lambda (x) (if (func x) (return-immediately x))) lst) #f))) (find-first integer? '(1/2 3/4 5.6 7 8/9 10 11)) ===> 7 (find-first zero? '(1 2 3 4)) ===> #f </syntaxhighlight> The following example, a traditional programmer's puzzle, shows that Scheme can handle continuations as first-class objects, binding them to variables and passing them as arguments to procedures. <syntaxhighlight lang="scheme"> (let* ((yin ((lambda (cc) (display "@") cc) (call-with-current-continuation (lambda (c) c)))) (yang ((lambda (cc) (display "*") cc) (call-with-current-continuation (lambda (c) c))))) (yin yang)) </syntaxhighlight> When executed this code displays a counting sequence: <code>@*@**@***@****@*****@******@*******@********...</code> <!-- Bear with me, I'm writing a clear English explanation of how this works, but it isn't easy. I'll add it when it's done. -->
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)