Fibonacci Perl Golf
There are plenty of Perl golf O(N) implementations of Fibonacci generators (and in other languages also), but I wondered in the shower today if I could do it without either an explicit initialisation or explicit previous variables. While I feel tracking both values in a single string is kindof cheating, this is the best I have come up with so far:
perl -le 'print$2while s/(\d*):?(\d*)/($1+$2||1).":$1"/e' |head -10
At 45 characters it's not a size winner (see this thread for implementations as short as 27 characters and 21 in Ruby) but I like it for a few reasons: it is complete (many other implementations skip the leading 1); it contains no initialisation (or indeed any implicit assigment operations); it uses only one state variable ($_).
Update: Perl golf is addictive! I had to let go of my "no implicit assignment" rule, but here is a similar 37 character solution. A weakness this version suffers from is that since it requires both single and double quote characters it's tough to fit into a shell one liner:
$_="$':".($'+$`||1),print$`while x./:/
Update 2: As pointed out on the related perlmonks thread, I can save a few chars with perl 5.10 using say:
perl -E 'say$2while s/(\d*):?(\d*)/($1+$2||1).":$1"/e' |head -10
$_="$':".($'+$`||1),say$`while x./:/
05:33 PM, 05 Oct 2008 by Mark Aufflick Permalink | Short Link








Perhaps...
...you should concentrate on cleaning in shower instead! :oP
by Unregistered Visitor on 10/08/08