Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Code is from Re^2: The Oldest Plays the Piano. As this is the second time someone challenges my fibonacci number generators, I'm starting to suspect I'm secretly writing these questions (under alternate names) to drive attention to my obfus and gain me more XP, and then forget about them so they don't weigh my conscience.

JavaFan already gave a good explanation about how the code works. The (?{$=++}) part in the regular expression counts how many times the part of the regular expression before that can match. Then the ^ anchor after that forces the expression to fail and backtrack trying another way to match the string.

There's one more thing to detail, and that's why the backreference is needed. If you just try this simple variant, you'll see it won't work.

perl -le'$==1,(1x$_)=~/^(1|11)*(?{$=++})^/,print$=for 0..20'
That's because the regular expression engine somehow memoizes where the (1|11)* subpattern could match, so it won't try to backtrack endlessly. The easiest way to fool the regular expression engine and make it do exponential amount of work would be to use a (??{}) or (?(?{})) expression. Because these features can run arbitary perl code and change whether the regular expression matches or not, there's no way the regex engine can correctly optimize them. Thus, this works.
perl -le'$==1,(1x$_)=~/^(1|11(??{}))*(?{$=++})^/,print$=for 0..20'

The \1 backreference (which matches the empty string here) is just a different way to do the same.

Update: Allow me some more explanation.

One more trick I didn't mention is that the regex is not anchored to the end of the string, so it counts matches to a prefix of the string only. It might be easier to understand the code if the match was anchored with a $ like this.

perl -le'$==0,(1x$_)=~/^(1|11(??{}))*$(?{$=++})^/,print$=for 0..20'
Note that in the original code, the $==1 is needed to add one to the number of matches so we get Fibonacci numbers, here it's not needed. As an example, the following shows all the ways /^(1|11)*$/ could match the string "111111".
1 (1)(1)(1)(1)(1)(1) 2 (1)(1)(1)(1)(1 1) 3 (1)(1)(1)(1 1)(1) 4 (1)(1)(1 1)(1)(1) 5 (1)(1)(1 1)(1 1) 6 (1)(1 1)(1)(1)(1) 7 (1)(1 1)(1)(1 1) 8 (1)(1 1)(1 1)(1) 9 (1 1)(1)(1)(1)(1) 10 (1 1)(1)(1)(1 1) 11 (1 1)(1)(1 1)(1) 12 (1 1)(1 1)(1)(1) 13 (1 1)(1 1)(1 1)
And here's how we get one less if the end of the regex is not anchored, so it can match only a prefix of the string.
1 (1)(1)(1)(1) 2 (1)(1)(1) 1 3 (1)(1)(1 1) 4 (1)(1) 1 1 5 (1)(1 1)(1) 6 (1)(1 1) 1 7 (1) 1 1 1 8 (1 1)(1)(1) 9 (1 1)(1) 1 10 (1 1)(1 1) 11 (1 1) 1 1 12 1 1 1 1

Update: see also Explain Fibonacci one-liner ?? for a duplicate of the same question and more answers.

In reply to Re: Explain one liner for fibonacci by ambrus
in thread Explain one liner for fibonacci by suhailck

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (7)
    As of 2018-06-19 14:26 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (114 votes). Check out past polls.