Beautiful.

I made a first try to traverse the tree recursively and then figured out that it does not work for comparing two trees, it seems impossible (or at least very complicated) to recurse on two data structures simultaneously.

So, I figured out that I probably needed an iterator (well actually two iterator functions) to return the leaves on demand, leading to a solution quite similar (at least in spirit) to yours, but, since I have done that only once or twice before, and quite some time ago, I had to go back to Mark-Jason Dominus' Higher Order Perl book to figure out how to do that. My iterators now seem to work more or less properly (although they are somewhat clumsy compared to yours), so that I am almost there (the main loop is not very complicated), but you did it before (and better).

Your code definitely looks much better than what I have so far, so that, unless I come with a brilliant new idea (unlikely), I will not submit mine;

Congratulations, roboticus. Beautiful work.

To LanX: using iterators is what is needed to make the process lazy (there may be other solutions, but this is a good one), or to make it possible to have it lazy. roboticus did not really make it completely lazy (the program is counting the number of differences, rather than exiting at the first difference), but it only takes a minor change to make it truly lazy (if we have the same understanding of lazyness).

BTW, roboticus, it would be good to publish your solution on the Rosetta Stone site: when you see solutions in some languages taking 200 lines of code or more, and a solution like yours taking more than 10 times less code, you get a certain picture of the amount of effort to get something done in various languages. ADA or Java, just to name two, are very powerful languages, no doubt, but need a lot of efforts to do something simple. In contrast, languages like Perl, Python, Haskell or Lisp (just a few examples) have much more expressive power. And, even thougn IT managers usually don't really understand the difference, they do if you tell them: "well this I can do in two weeks with XYZ super-duper object language , and in 2 days in Perl or Haskel (or Lisp, or whatever).


In reply to Re^2: Challenge: Perl 5: lazy sameFinge()? by Laurent_R
in thread Challenge: Perl 5: lazy sameFringe()? by BrowserUk

Title:
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, details, 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, summary, 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.