On Wed, Aug 26, 2020 at 10:33 AM Tobias Boege <t...@taboege.de> wrote:
>
> 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


Thank you, Tobias! Here's my take (REPL, below):

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

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

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

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

WOW. I never would have expected the last line of code above to return
a sequence starting "1, 0, 1 ..." up to 19.

Would I ever want that sequence? Couldn't I get that sequence just as
easily by asking for a sequence of (-1 ... ^20), and then calling
abs() on each element (using hyper)? See below:

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

Anyway, I opened a Github issue, and (like yourself here), Jonathan
has graciously replied. Feel free to chime in at:

https://github.com/rakudo/rakudo/issues/3881

Thanks again, Bill.

Reply via email to