Racket – iterazioni e comprehensions – 2

kk9Continuo da qui, copiando qui: [doc]/guide/for.html.

for e for*

A more complete syntax of for is

(for (clause ...)
  body ...+)
  clause = [id sequence-expr]
         | #:when boolean-expr
         | #:unless boolean-expr

When multiple [id sequence-expr] clauses are provided in a for form, the corresponding sequences are traversed in parallel:

ic8

With parallel sequences, the for expression stops iterating when any sequence ends. This behavior allows in-naturals, which creates an infinite sequence of numbers, to be used for indexing:

ic9

The for* form, which has the same syntax as for, nests multiple sequences instead of running them in parallel:

ic10

Thus, for* is a shorthand for nested fors in the same way that let* is a shorthand for nested lets.

The #:when boolean-expr form of a clause is another shorthand. It allows the bodys to evaluate only when the boolean-expr produces a true value:

ic11

A boolean-expr with #:when can refer to any of the preceding iteration bindings. In a for form, this scoping makes sense only if the test is nested in the iteration of the preceding bindings; thus, bindings separated by #:when are mutually nested, instead of in parallel, even with for.

ic12

An #:unless clause is analogus to a #:when clause, but the bodys evaluate only when the boolean-expr produces a false value.

for/list e for*/list

The for/list form, which has the same syntax as for, evaluates the bodys to obtain values that go into a newly constructed list:

ic13

A #:when clause in a for-list form prunes the result list along with evaluations of the bodys:

ic14

This pruning behavior of #:when is more useful with for/list than for. Whereas a plain when form normally suffices with for, a when expression form in a for/list would cause the result list to contain #<void>s instead of omitting list elements.

The for*/list form is like for*, nesting multiple iterations:

ic15

(nota: copincollando nella REPL salta un po’ l’indentazione, purtroppo)

A for*/list form is not quite the same thing as nested for/list forms. Nested for/lists would produce a list of lists, instead of one flattened list. Much like #:when, then, the nesting of for*/list is more useful than the nesting of for*.

Continua :mrgreen:

Posta un commento o usa questo indirizzo per il trackback.

Trackback

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo di WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

Google photo

Stai commentando usando il tuo account Google. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

Connessione a %s...

Questo sito utilizza Akismet per ridurre lo spam. Scopri come vengono elaborati i dati derivati dai commenti.

%d blogger hanno fatto clic su Mi Piace per questo: