[racket-dev] for behavior with empty sequence

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Sun Nov 7 20:40:52 EST 2010

At Sun, 07 Nov 2010 17:13:26 -0700, Jon Rafkind wrote:
> Is this the right behavior for a `for' loop when no sequences are given?
> -> (for () (printf "ok!\n"))
> ok!

Yes, as documented.

> I would expect the body to occur 0 times.

As far as I can tell, 1 is more consistent.

The easiest explanation for 1 times is that it's the right base case
for `for*'. Clauses of ` for*' multiply, rather than add; you get 1
iteration for the empty sequence in the same way that you get 1 for the
product of an empty set of numbers. A `for*' with a empty sequence, in
contrast, is analogous to a set of numbers that includes 0.

Although `for*' is documented in the reference by transformation to
`for', the transformation highlights how `for' is really a kind of
`for*'. If you throw `#:when' out of `for', so that it's not at all
`for*', then I think it's most consistent to rule out the case of zero
sequences for `for'. (The number of iterations for a plain `for' is the
minimum item count of the given sequences, and "minimum" needs a
non-empty set.) In that case, the behavior of `for' on zero sequences
is arbitrary --- and so we pick the answer that makes `(for () ....)'
the same as `(for* () ....)' and that's consistent with `#:when'.

Posted on the dev mailing list.