Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://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

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-18 20:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found