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
Generator (computer programming)
(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!
===Racket=== [[Racket (programming language)|Racket]] provides several related facilities for generators. First, its for-loop forms work with ''sequences'', which are a kind of a producer: <syntaxhighlight lang="racket"> (for ([i (in-range 10 20)]) (printf "i = ~s\n" i)) </syntaxhighlight> and these sequences are also first-class values: <syntaxhighlight lang="racket"> (define 10-to-20 (in-range 10 20)) (for ([i 10-to-20]) (printf "i = ~s\n" i)) </syntaxhighlight> Some sequences are implemented imperatively (with private state variables) and some are implemented as (possibly infinite) lazy lists. Also, new struct definitions can have a property that specifies how they can be used as sequences. But more directly, Racket comes with a generator library for a more traditional generator specification. For example, <syntaxhighlight lang="racket"> #lang racket (require racket/generator) (define (ints-from from) (generator () (for ([i (in-naturals from)]) ; infinite sequence of integers from 0 (yield i)))) (define g (ints-from 10)) (list (g) (g) (g)) ; -> '(10 11 12) </syntaxhighlight> Note that the Racket core implements powerful continuation features, providing general (re-entrant) continuations that are composable, and also delimited continuations. Using this, the generator library is implemented in Racket.
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)