|Do you know where your variables are?|
The Lighter Side of Perl Culture (Part IV): Golfby eyepopslikeamosquito (Bishop)
|on Mar 06, 2005 at 12:30 UTC||Need Help??|
Guided by a core set of principles and goals, every language designer is tormented by thousands of agonizing choices. Gosling chose differently to Wall. A perhaps unintended side effect of some of Larry's choices -- such as Perl's "diagonal-ness" and TMTOWTDI -- is that playing golf in Perl is fun. By contrast, and I've never tried it, I imagine playing golf in Java (or Cobol) would feel oppressive in the extreme.
Of course, like obfuscation and poetry, only a minority of Perl programmers actually pursue golf as a hobby. James Gosling, for instance, though he readily acknowledges writing many Perl programs, has never entered a Perl golf tournament, to the best of my knowledge.
This, the fourth episode of the long running series on the lighter side of Perl culture, focuses on Perl Golf.
As indicated by the quotes above, playing golf in computer languages started long before Perl did. It's just that it wasn't called golf back then.
Perl's main claim to fame in this golf business is that a Perl hacker, namely Greg Bacon, actually coined the term Perl Golf in a comp.lang.perl.misc thread on 28 May 1999. And the term golf has been applied to other computer languages since then. Ironically, Greg may have intended the term as derogatory -- just as Fred Hoyle coined the term "Big Bang" to mock that theory -- because Greg once dismissed golf as really, really boring.
As you might expect, the earliest exponents of Perl Golf were probably Larry and Randal. For example, here is an early golfish Larry post from March 1990. And in this one Larry implies that Randal was his golfing buddy in those early days.
In March 1995, Adam Back and Hal Finney caused a stir by implementing RSA encryption and decryption in Perl as a signature file. Later Clifford Adams and others joined in the fun.
In September 1995, in what was probably the first golf contest with money prizes, the surprising winner of this comp.programming.contests C golf contest was a Perl hacker (some might say the Perl hacker;-). The goal of this competition was to find the shortest C program that reverses all words in a file. I found this exchange amusing.
Jumping forward to July 2000, Uri Guttman initiated the ill-fated Perl Golf Apocalypse scheduled to run, accompanied by witty commentary, in front of a live audience at TPC 4. Alas, a number of technical glitches caused the event to be cancelled, replaced by a Damian Conway talk. Given that Larry was in the audience, it's little wonder that Uri is still scarred by it today. To my knowledge, no one has been brave enough to attempt a live Perl Golf contest since. The Apocalypse questions formed part of a golf competition run by jmcnamara in PGA Golf, Round 1 and PGA Golf, Round 1 - Results here at the monastery. I note with interest that mtve found some improvements to the original contest solutions some years later (this seems to be a speciality of mtve and thospel).
During 2001, there was such a rush of golf posts here at the monastery, that chipmunk sent out a plea, Tips on Writing a Golf Challenge, to calm things down a bit. Despite Perl Golf - A proposal for a new section of PerlMonks.org PM never did get its own dedicated golf section.
Just before Christmas 2001, the fwp mailing list exploded with a flurry of golf threads, starting with the Santa Claus Golf Apocalypse won by Eugene van der Pijll. This tournament introduced an innovation: strict rules and a test program that all entries must pass before being accepted. A much improved version of this early test program can be found here. These golf contests spawned a new mailing list firstname.lastname@example.org, split off from fwp.
A new magazine, The Perl Review, launched early in 2002, sponsored a series of golf tournaments run at SourceForge by Dave Hoover and Jérôme Quelin. These tournaments were run monthly throughout 2002, the top five money winners at the end of season being:
In 2003, the TPR tournaments moved to Terje Kristensen's minigolf site where Ton Hospel, Mtv Europe and Rick "tybalt89" Klement continued to dominate. Around this time, golf enjoyed a surge of popularity in Poland, courtesy of games run at the kernelpanic.pl web site and the Polish comp.lang.perl newsgroup. After two to three seasons, these sites became inactive and the game of Perl Golf transmogrified into Code Golf.
Serious golf competitions in Perl, Python, Ruby and PHP are nowadays run at Code Golf (update: now at code-golf.io with github login), while less serious ones (in 51 different languages) are played at shinh's golf site.. Update: As indicated at The golf course looks great, my swing feels good, I like my chances (Part IV), code golf is much more popular in Python than Perl nowadays.
What is Perl Golf?
Is it an art, a sport, just a bit of fun, a waste of time, or a threat to sound coding practices? I don't want to dwell too much on this; it is different things to different people. Hopefully, as with obfuscation, people are sensible enough to not start golfing on production code. My personal view is that golf is mostly about fun and competition.
As for the usefulness of Perl golf, apart from being a fun way to explore TMTOWTDI, notice that Perl golf, by its nature, pushes the language to its limits, often exercising boundary conditions. It should come as no surprise therefore that quite a few perl bugs have been unearthed during golf tournaments, especially by Ton Hospel. Update: and much later by Discipulus.
Competitions requiring judges, such as Olympic gymnastics and obfuscated Perl competitions, are easily spoiled by poor judging, as alluded to above by MJD. The wonderful thing about Perl Golf competitions compared to obfuscated ones is that golf is typically judged simply by counting up the number of characters in your solution. I've never seen any ugly disputes or complaints about who should have won.
Of course, the winning golf solution may be horribly ugly or highly inefficient. So several people have suggested alternative types of competitions, judged by different criteria. Perhaps the most commonly suggested is the 100 metre dash where each solution is timed and the fastest wins. A novel twist on this idea is to measure not the CPU or wall clock time, but the number of Perl ops. For more information on Perl ops see Nicholas Clark's excellent paper "when perl is not quite fast enough". Yet another alternative is MJD's Perl Quiz of the Week where human judges comment on the "quality" of a solution after a 60 hour grace period. Update 2019: As mentioned at Weekly Challenge, manwar has started a Perl Weekly Challenge, where, similar to MJD's Perl Quiz of the Week, humans comment on interesting solutions submitted. Also, bliako suggested Perl Monks host coding contests: Coding challenges to PM.
It's a mark of Capablanca's genius that he was able to refute over the board a variation his grandmaster opponent had been studying and preparing for nine years.
In similar style to Capablanca, during the TPR tournaments four expert golfers would sometimes cook up a problem, study it in consultation for a month, and conclude the best solution possible was, say, 120 strokes. Yet within a couple of hours, the remarkable Ton Hospel would then go and post a score of around 100!
In many ways, Perl Golf tournaments resemble a fiercely contested chess tournament.
Some Classic Golf Games
One golf that seems to come up again and again is the famous 99 bottles of beer song. It's been discussed here at the monastery in: 99 bottles, 2 lines, 99 Bottles Of Beer (can't we do better), 99 bottles of beer on the Wall, Golf: Back to those damn beer bottles again... and Bottle Golf. For the record, I believe the shortest known solution is not in any of those nodes, but was concocted by thospel and mtve as follows:
This version takes a command line argument specifying the number of bottles (default 99). If you find a shorter one, respond away! May 2011 Update: Shorter bottle golf solutions are now described in Drunk on golf: 99 Bottles of Beer.
The most interesting golf games occur when several solutions, all with a similar score, are possible. To illustrate, consider TPR(0,5a), an infix to postfix converter. This problem could be solved with: an operator stack; via overload; a recursive regex; top-down parsing; or Eugene's winning Mad Dutch algorithm (invented for this game). Sometimes, a truly astonishing approach is found, as when Jukka Suomela in TPR(0,4b) observed that perl itself has a built-in topological sorter: the garbage collector. Chris Dolan's explanation can be found here.
A crucial skill for serious golfers is the dark art of unearthing magic formulae. Though he didn't call them "magic formulae" back then, Ton Hospel invented them back in 2002. In 2006, he further popularized them by unleashing a mind-bogglingly complex Decimal-to-Roman magic formula to win the $350 cash prize on offer in the 2006 Fonality Christmas golf challenge. Incredibly, primo found a one-stroke improvement to Ton's masterpiece six years later. Proving this was no fluke, primo made a massive improvement to another ancient Ton Hospel winning golf solution.
Inspired by Ton, and emphasizing their importance in golf, I concocted four different magic formulae in four different languages (Perl, Ruby, Python, PHP), to win the follow-up Roman-to-Decimal golf challenge at the code golf web site. That is, in all four languages, the key component was not the code, but the magic formula.
Perversely, searching for a magic formula can sometimes transmogrify a golf challenge from writing the shortest program to writing the fastest one! Examples of this can be found in the Ton Hospel quote above and in The 10**21 Problem (Part I).
Compression in Golf
The first golfers to employ code compression techniques in golf were Marko Nippula of Finland and Stefan 'Sec' Zehl of Germany in 2002. In the TPR(0,4) "Interlinear Counts" golf competition, played during the first week of June 2002, Marko submitted a 169-stroke "pack u" compressed solution and 'Sec' a 244-stroke one, both well behind Ton Hospel's (uncompressed) winning score of 150. This controversial technique was banned in subsequent Perl golf competitions in 2002, but is widely used in code golf competitions today, as detailed in Compression in Golf: Part I.
Some Standard Golfing Techniques
An excellent introduction to the basics of shortening your Perl code be found in Perl Golf 101.
References Added Later
Other Articles in This Series
Updated Aug 2011: Added John McCarthy (1959) and RSA (1995) reference; Dec 2013: added ySas' device; Oct 2014: Added new "Magic Formulae" and "Compression in Golf" sections. April 2019: Added reference to Weekly Challenge. Added note that Python golf has become more popular than Perl golf.