Difference between for and for*

Hello,
I’m working with for expressions.
In these examples in the racket guide sometimes it is used for and sometimes for*

and I can’t understand why.

Matteo

You are reading the section "11.9 Breaking an Iteration", which is toward the end of the guide. Have you read earlier sections (particularly "11.2 for and for*") to understand how for and for* are different?

Hi,
yes, I’ve read it, but I still can’t understand the examples I’ve reported.

Matteo

Hi @matteo-daddio ,

The title of your post evokes the difference between for and for*, while the examples you cite focus on breaking conditions, i.e. facilities to stop iteration early, once some conditions are satisfied.

Based on this observation, I suggest that you come back to 11 Iterations and Comprehensions , which gives the following answer to the question in the title of your post:

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

In other words, when you have two variables in a for iteration, both advance at each step. On the other hand, when you have two variables in a for* iteration, the second (innermost) variable traverses the whole of its sequence for each value the first variable takes in its own sequence. The first two examples in Section 11.2 I point to above should be rather illustrative.

Once you are all set with the difference between for and for*, the examples you include in original post should click.

1 Like

Hello,
the post title I’ve chosen is a bit misleading. I think I’ve understood clearly the difference between for and for* and I think there’s a bug in the racket guide.

If you look carefully the examples I posted I think you’ll notice that all the for expressions should be for*.

Am I wrong?

Matteo

What you are confused about is the behavior of #:when and #:unless within for, but this is already explained in “11.2 for and for*”.

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.

In other words, when there’s a #:when or #:unless, for clauses across the boundary are no longer iterated in parallel, but they become nested similar to for*.

1 Like

Thank you very much. I missed that. Now it’s clear. Thanks for the reply.

Matteo