Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Beyond Golf - reading between the tokens

by Aristotle (Chancellor)
on Dec 28, 2002 at 00:42 UTC ( #222656=perlmeditation: print w/ replies, xml ) Need Help??

One thing that always irks me about the definition of Golf,
A competition to find the shortest (fewest keystrokes) Perl code to solve a given problem.
is how it hinges on the length of the representation of the solution. That's an interesting challenge, obviously, but most of the time I'd be far more interrested in a different challenge:
A competition to find the shortest Perl code in terms of tokens to solve a given problem.

where a token is one thing that represents a single semantic entity: Obviously deciding on precise criteria is more involved - it's obvious that functions and keywords count as one token, variables are harder to grasp since $t and $$t[0]->{x} shouldn't both count as a single token. The clearest rule seems to be that you can use exactly one sigil. Every additional sigil and pair of brackets/curlies counts as another token, but arrows count for naught since they're just syntactical sugar. The index inside the brackets/curlies is an extra token by itself, too (being the simplest form of expression). Finally, all pairing symbols, like the curlies that delimit a BLOCK or the brackets that construct an anonymous array, count as a single token (you can't leave out one of them). Most parens don't count - those that belong to a function call and others are only there for disambiguation and don't alter the meaning (or, the intent) of the code.

Basically, this would be a competition to say something in the fewest words possible, as opposed to the Golf challenge to say something in the fewest letters possible.

What do you think? Would such a competition be interesting? Are there loopholes in the rules, are more rules needed? What should it be named? How many questions can I ask?

Makeshifts last the longest.

  • Comment on Beyond Golf - reading between the tokens

Replies are listed 'Best First'.
Re: Beyond Golf - reading between the tokens
by gjb (Vicar) on Dec 28, 2002 at 01:27 UTC

    1. That will cost you a penny ;-)
    2. Such a competition would be nice since it's more "semantic" than "pure golf" and should yield nicer code.
    3. What about the semi-colon, I'd guess it doesn't count? It should be possible to automate the count using a lexer for Perl if it isn't sensitive to $$a[0] vs. $a->[0] et al. differences.
    4. How about Croquet?
    5. At least 5, but I guess you held back a little ;-)

    How about Polo, getting the task done fastest using

    Just my 2 cents, -gjb-

      The semicolon is an interesting case. I'm leaning towards not counting it, as I can't think of a case where the shorter of two solutions would turn out to be longer depending on semicolon sensitivity. If such a case exists, this would take further thought.

      As for automated counting, my idle musing was to abuse the Perltidy code for that purpose.

      I don't like the idea - you'd have to specify the exact environment (Perl version+patches, OS, hardware, modules' versions) to be able to decide on a reproducibly and unambiguously winning entry. It's just too fickle a goal.

      Makeshifts last the longest.

        The idea could be done if the code were submitted to run on a particular computer, if someone is crazy enough to risk something like that ;-)

        Seriously, it could be done in the sense that everyone is able to confirm the results on his own machine with its specific setup. The format of submission would be a function that can be run using Benchmark so that the "competition" could be compared by just pasting the code and running the benchmarks.

        I agree that you won't see a clear winner, but it would give those of us interested in raw performance a number of alternative implementations and hence an opportunity to learn. It's all about taking part, not about winning ;-)

        Just my 2 cents, -gjb-

        Update: It would be possible to get a machine/OS/etc. independent comparison by using some "gauge code", ie. a standard piece of code. The benchmark for the Perl Polo code would then be calculated proportional to the time it takes the "gauge code" to execute.

Re: Beyond Golf - reading between the tokens
by MarkM (Curate) on Dec 28, 2002 at 07:44 UTC

    Like obfuscation, golf is an art. Most excellent golf entries are the result of evolution, experimentation, and creativity - the solution is not approached the same way one would approach a practical programming problem. Fewest characters is actually a sensible criteria as it adds a fun twist that skews the rules sufficiently to ensure that the game is not boring. Experienced programmers no longer have an untouchable edge when it comes to golf.

    If another scoring mechanism was used, my personal preference would be - fewest nodes in the compiled op tree. Of course, this breaks down when eval"" is used, but the idea still stands - the eval"" case is just hard to calculate.

      Yes, I thought about eval, that would need special rules indeed. The "nodes in the compiled tree" approach is a good proposal - I was trying to formulate the rules in such a way as to be unambiguous, and that would be a watertight way to get it right. Kudos to Juerd for a great suggestion on how to implement that with very little effort.

      As to the point, it's true that these rules favour experienced programmers, but then these folks tend to be hard to beat in the pro league anyway. What usually leaves me unsatisfied at least about most simpler golf challenges is that concise in terms of expressiveness approaches tend to be too long when written out, so simplistic, brute force approaches, often really hackish ones, are favoured.

      Basically, what I'd like to see is a challenge to use the language's expressive power to the maximum.

      Makeshifts last the longest.

Re: Beyond Golf - reading between the tokens
by Juerd (Abbot) on Dec 28, 2002 at 12:51 UTC

    I think much easier would be to just:

    perl -MO=Concise -e'foo' | wc -l
    5;0 juerd@atlas:~$ perl -MO=Concise /usr/bin/cpanp | wc -l /usr/bin/cpanp syntax OK 260
    The cpanp binary has a score of 260.

    5;0 juerd@atlas:~$ perl -MO=Concise -e'$t' 2> /dev/null | wc -l 5 5;0 juerd@atlas:~$ perl -MO=Concise -e'$$t' 2> /dev/null | wc -l 6 5;0 juerd@atlas:~$ perl -MO=Concise -e'$$t[0]->{x}' 2> /dev/null | wc +-l 11 5;0 juerd@atlas:~$ perl -MO=Concise -e'$$t[0]{x}' 2> /dev/null | wc -l 11 5;0 juerd@atlas:~$ perl -MO=Concise -e'print $$t[0]{x}' 2> /dev/null | + wc -l 13

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.

      I thought of that too, but it does have some problems. Eval-string is very cheap in that accounting. print 'a'.'b'; is cheaper then print 'a','b'; (the former constant-folds to print 'ab';).

      Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

        I concur with Juerd - the costs are assesed correctly if you throw non-trivial cases into the equation. As far as eval STRING is concerned, I'm wondering if it shouldn't simply be forbidden.

        Makeshifts last the longest.

        I thought of that too, but it does have some problems. Eval-string is very cheap in that accounting. print 'a'.'b'; is cheaper then print 'a','b'; (the former constant-folds to print 'ab';).

        This is why print "a"."b"; is faster and more efficient than print "a","b";. I think it is *good* to have the former be less points.

        When not using constants, it's a different story: print "a".$_; is 9 concise-lines while print "a",$_; is 8.

        I think eval-string should be avoided anyhow, in a golf contest like this one.

        - Yes, I reinvent wheels.
        - Spam: Visit eurotraQ.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://222656]
Approved by gjb
Front-paged by wil
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2016-08-24 22:48 GMT
Find Nodes?
    Voting Booth?
    The best thing I ever won in a lottery was:

    Results (351 votes). Check out past polls.