in reply to Re: 99 Problems in Perl6 (Lisp, Prolog, Haskell)
in thread 99 Problems in Perl6

Here's a prettier way to write the Perl 6 solution:
sub group (*@array is copy) { gather { while @array { take [ gather { my $h = shift @array; take $h; while @array and $h eq @array[0] { take shift @array; } } ]; } } }
It would be slightly prettier if take could harvest a value en passant.

Replies are listed 'Best First'.
Re^3: 99 Problems in Perl6 (Lisp, Prolog, Haskell)
by Ovid (Cardinal) on Dec 15, 2006 at 23:12 UTC

    Sweet!

    Why do you flatten the array? I played with that in Pugs and the asterisk seems superfluous. It seems like one is copy the elements and the other is copying the array. I thought that maybe your version would avoid aliasing problems, but even in checking that, I'm not seeing it happening.

    sub group (@array is copy) { # didn't flatten gather { while @array { take [ gather { my $h = shift @array; take $h; while @array and $h eq @array[0] { take shift @array; } } ]; } } }

    Cheers,
    Ovid

    New address of my CGI Course.

      I just chose to write it as a list operator so that you could say
      @result = group 1,2,2,2,3,3,4,4,4,4,5,5,6,6;
      instead of doing it all with "scalar" arrays:
      @result := group [1,2,2,2,3,3,4,4,4,4,5,5,6,6];
      But either approach is fine.