Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Scheme (programming language)
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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. -->
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Scheme (programming language)
(section)
Add topic