Normalmente si usano parole diverse, per esempio cicli ma iterazioni va benissimo. È tutto qui:
The for family of syntactic forms support iteration over sequences. Lists, vectors, strings, byte strings, input ports, and hash tables can all be used as sequences, and constructors like
in-range offer even more kinds of sequences.
for accumulate iteration results in different ways, but they all have the same syntactic shape. Simplifying for now, the syntax of for is
(for ([id sequence-expr] ...) body ...+)
for loop iterates through the sequence produced by the
sequence-expr. For each element of the sequence,
for binds the element to
id, and then it evaluates the
bodys for side effects.
for/list variant of
for is more Racket-like. It accumulates body results into a list, instead of evaluating body only for side effects. In more technical terms,
for/list implements a list comprehension.
The full syntax of
for accommodates multiple sequences to iterate in parallel, and the
for* variant nests the iterations instead of running them in parallel. More variants of
for* accumulate body results in different ways. In all of these variants, predicates that prune iterations can be included along with bindings.
Before details on the variations of
for, though, it’s best to see the kinds of sequence generators that make interesting examples.
Costruttori di sequenze
in-range function generates a sequence of numbers, given an optional starting number (which defaults to
0), a number before which the sequence ends, and an optional step (which defaults to
1). Using a non-negative integer
k directly as a sequence is a shorthand for
in-naturals function is similar, except that the starting number must be an exact non-negative integer (which defaults to
0), the step is always
1, and there is no upper limit. A for loop using just
in-naturals will never terminate unless a body expression raises an exception or otherwise escapes.
La call a
error è, forse, di solito eccessiva, ecco cosa capita in DrRacket
Se creo lo script
ci.rkt e lo eseguo ottengo:
#lang racket (for ([i (in-naturals)]) (if (= i 10) (error "\ntoo much!\n") (print i))) (newline)
fissare scoprire come rendere usabile 😉
In realtà basta accedere alla reference di
error, da fare 🙄
Oppure come da paragrafo immediatamente successivo, più semplice.
stop-after functions construct a new sequence given a sequence and a predicate. The new sequence is like the given sequence, but truncated either immediately before or immediately after the first element for which the predicate returns true.
Sequence constructors like
in-string simply make explicit the use of a list, vector, or string as a sequence. Along with
in-range, these constructors raise an exception when given the wrong kind of value, and since they otherwise avoid a run-time dispatch to determine the sequence type, they enable more efficient code generation; see Iteration Performance [prossimamente] for more information.
Sequences in The Racket Reference provides more on sequences.
Pausa 😀 mi sa che il discorso sarà lungo