I am posting this as a challenge, mostly because I lack the perl skills to do it myslef, and I think that it would be a neat chunk of code.

I prepose developing a perl script that could golf perl code. I by no means imply that it is possible to write it in such a way as to rival human golfers, but there are several mechanical substitutions, such as replacing shift with pop, that perls ability at text manipulation should be able to make quick work of. This would eliminate the first couple of steps in golfing and leave just the creative manipulations for the expirienced golfer.

I think japhy's Golf Do's and Don'ts would make a good start for a specification. Here is some code for replacing shift with pop:

@code = ('shift($a, $b);', 'print @;'); foreach $line(@code){ $a=$line; for($a=~ m/shift\((.*?)\)/){ $a=~ s/shift\(//; $a=~s/\)\;//; @a=split(/,/ ,$a); @a = reverse(@a); $line=~ s/shift\((.*?)\)/pop(@a)/; } } print @code, "\n";

Ideas, opinions, contributions welcome.

Replies are listed 'Best First'.
Re: Challenge: Golfing Golf
by zentara (Cardinal) on Mar 05, 2005 at 13:00 UTC
    I'm not much of of "golfer" myself, but after watching the best do it, for a few years, I can see that it is an "art" or "skill". Being such, it would be impossible to write a script to do it effectively.

    It is just like "real golf". A machine could probably be constructed to "hit the ball", and "eyeball measurements:, etc, and it probably could consistently produce a mediocre game score. But to get those low scores, which win, takes some "artistic thought", which has eluded AI programmers for years.


    I'm not really a human, but I play one on earth. flash japh
Re: Challenge: Golfing Golf
by spurperl (Priest) on Mar 04, 2005 at 18:51 UTC
    Such a program with any aspiration for success will have to know how to parse Perl code.

    Your sample code, for instance, will work for only some scripts, and fail (i.e. change the program logic) for others.

      Hmmm...but "only Perl can parse Perl."
Re: Challenge: Golfing Golf
by 5mi11er (Deacon) on Mar 09, 2005 at 00:13 UTC
    Recalling an article I read in the not terribly distant past about an experiment in trying to hard code an eeprom chip to recognize (notes, sounds, voices, can't recall exactly) it was said to be quite difficult because it took "forever" for the sound to happen compared to the speed of the processor. In the end they did some AI type training, and let the chip evolve using various techniques in both sexual and asexual reproductions etc.

    I think, in the end, in order for any "mechanically created" golf attempts to have a shot at competing with humans, a similar process would have to be followed for each and every golf challenge.

    But, it might be possible for someone to rig up a flexable automatic system for (breeding / culling / mutating / scoring / etc) golf attempts...

    -Scott