Here I place a Prolog procedure to encode the runlength of a list of symbols. For example:
[12,2,2,w,3,3,s,s,s,] maps to [1*12,2*2,1*w,2*3,3*s]
runcode([],C,N,[N*C]). runcode([H | T],H,N,Z) :- N1 is N+1,runcode(T,H,N1,Z). runcode([H | T],C,N,[N*C| Z]) :- H\==C,runcode(T,H,1,Z).

Replies are listed 'Best First'.
Re: PPGA - Prolog-Perl Golf Association: Run-length encoding
by scain (Curate) on Jul 02, 2001 at 19:10 UTC
    Is this like a zen meditation? You know, where the zen master asks you a question that makes no sense so you can free your mind? I don't understand this at all.

    Scott

Re: PPGA - Prolog-Perl Golf Association: Run-length encoding
by japhy (Canon) on Jul 02, 2001 at 20:32 UTC
    I find a Perl solution a bit more readable:
    sub runcode { my @ret; while (@_) { my ($x, $c) = (shift, 1); $c++, shift while $_[0] eq $x; push @ret, "$c*$x"; } return @ret; }
    If you want to golf this:
    # 78 chars sub RC { #123456789_123456789_123456789_123456789_123456789_123456789_123456789 +_12345678 my@r;while(@_){my($x,$c)=(shift,1);$c++,shift while$_[0]eq$x;push@r," +$c*$x"}@r }
    I'm sure it can be shrunk, but I'm kinda busy right now.

    japhy -- Perl and Regex Hacker
Re: PPGA - Prolog-Perl Golf Association: Run-length encoding
by clemburg (Curate) on Jul 02, 2001 at 20:30 UTC

    Well, OK.

    (Why do I respond to something like this?)

    This is your Prolog ported to Perl. Now somebody else can make that shorter ;-) ...

    #!/usr/bin/perl -w use strict; my $input = [12, 2, 2, 'w', 3, 3, 's', 's', 's']; my ($head, @tail) = @{$input}; print "@{runcode(\@tail, $head, 1, [])}"; sub runcode { my ($tail, $current_head, $count, $output) = @_; return [@{$output}, "$count*$current_head"] unless @{$tail}; my ($head, @tail) = @{$tail}; if ("$current_head" eq "$head") { return runcode(\@tail, $head, ++$count, $output) } return runcode(\@tail, $head, 1, [@{$output}, "$count*$current_head"]); }

    Christian Lemburg
    Brainbench MVP for Perl
    http://www.brainbench.com

Re: PPGA - Prolog-Perl Golf Association: Run-length encoding
by chipmunk (Parson) on Jul 02, 2001 at 20:55 UTC
    Here's another golfed Perl solution, at 67 characters:
    sub rle { ($c)=@_;my$i;map$c ne$_?("$i*$c",$i=1,$c=$_)[0]:++$i&&(),@_,!$_[-1] }
      Hmm... this appears signigicantly shorter than the original Prolog. Looks like it's PrincePawn's turn to take a swing at the ball :)

      Just goes to show what a flexible language Perl is.

      TMTOWTSAC!

Re: PPGA - Prolog-Perl Golf Association: Run-length encoding
by tye (Sage) on Jul 02, 2001 at 22:46 UTC

    I didn't like any of the solutions so far since none of them handle input in the same format as their own output.

    #!/usr/bin/perl -w use strict; sub RunLengthEncode { my @out; while( @_ ) { my $next= shift; if( ! ref($next) ) { $next= {count=>1,value=>$next}; } if( @out && $out[$#out]{value} eq $next->{value} ) { $out[$#out]{count} += $next->{count}; } else { push @out, $next; } } return @out; } for( @ARGV ) { print join ",", map { $_->{count}."*".$_->{value} } RunLengthEncode( split /,/, $_ ); print $/; }

    But that doesn't define a bunch of rules for "reducing" a problem because, well, it wasn't written in Prolog. (:

            - tye (but my friends call me "Tye")
Re: PPGA - Prolog-Perl Golf Association: Run-length encoding
by MeowChow (Vicar) on Jul 02, 2001 at 22:06 UTC
    None too pretty, but here's one that gets the job done at 58:
    sub rle { my$c;map{shift;$c++;$_[0]ne$_?do{$a="$_*$c";$c=0;$a}:()}@_ }
    Though, it has some "issues"...
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print
Re: PPGA - Prolog-Perl Golf Association: Run-length encoding (take 2)
by MeowChow (Vicar) on Jul 04, 2001 at 10:55 UTC
    Make that 53 (and strict, to boot) ... ;-)
    sub rle { my$c;map{shift;$c++;$_[0]ne$_?("$_*$c",$c=0)[0]:()}@_ }
    update: err.. 51
    sub rle { my$c;map{shift;$c++;$_[0]ne$_?"$_*$c"|($c=''):()}@_ }
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print