Content deleted Content added
Demonkoryu (talk | contribs) Reverted 1 edit by 208.54.44.150 (talk). (TW) |
Elibarzilay (talk | contribs) No edit summary |
||
Line 155:
IO.WriteLn "I=", I
</source>
=== 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:
<source lang="scheme">
(for ([i (in-range 10 20)])
(printf "i = ~s\n" i))
</source>
and these sequences are also first-class values:
<source lang="scheme">
(define 10-to-20 (in-range 10 20))
(for ([i 10-to-20])
(printf "i = ~s\n" i))
</source>
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,
<source lang="scheme">
#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)
</source>
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.
===Tcl===
Line 319 ⟶ 347:
[[Category:Articles with example Perl code]]
[[Category:Articles with example Tcl code]]
[[Category:Articles with example Racket code]]
[[fr:Générateur (informatique)]]
|