On Wed, 26 Aug 2020, William Michels via perl6-users wrote:
> > They can be pretty great, especially when combined with the magic op= 
> > operators that (in essence) know about identity elements.  I've done a few 
> > challenges on the Code Golf Stackexchange site where I wanted an infinite 
> > sequence like this:
> >
> >     0, 1, -2, 3, -4, 5, -6, ...
> >
> > It took me a while to realize this can be expressed in Raku simply as:
> >
> >     { $++ * ($ *= -1) } ... *
> >
> 
> Hi Sean, that's a neat solution! I seem to have found an oddity though
> (whitespace sensitivity). In the REPL:
> 
> > say { $++ * ($ *= -1) } ...21
> (0 1 -2 3 -4 5 -6 7 -8 9 -10 11 -12 13 -14 15 -16 17 -18 19 -20 21)
> > say { $++ * ($ *= -1) } ...^21
> (0 1 -2 3 -4 5 -6 7 -8 9 -10 11 -12 13 -14 15 -16 17 -18 19 -20)
> > say { $++ * ($ *= -1) } ... 21
> (0 1 -2 3 -4 5 -6 7 -8 9 -10 11 -12 13 -14 15 -16 17 -18 19 -20 21)
> > say { $++ * ($ *= -1) } ... ^21
> (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20)
> 
> I would have expected the last line of code (whitespace between "..."
> and "^21") to give the same answer as the first three. Any ideas?
> 

The difference is likely to come from the fact that "^20" as a term is
a shorthand for the range "0..^20", while "...^" is the sequence operator
with exclusive endpoint. Placement of whitespace will dictate where the
little hat attaches to and thus change the meaning of the statement.

Observe:

  > 1 ...^ 20
  (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19)

  > 1 ... ^20  # actually C«1 ... (0..19)»
  (1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19)

The documentation [1] states that the C«...» infix is list-associative
and while I have never seen an example of that (including in that docs
page), it would explain why it happily takes in your list (1) and then
your list (0 .. 19) and concatenates them into a sequence, without
applying any of the usual sequence operator magic.

When you write "1 ...^20" I suppose that longest-token matching prefers
the former interpretation as it makes the infix token longer.

Best,
Tobias

[1] https://docs.raku.org/language/operators#index-entry-..._operators

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk

Reply via email to