|Perl Monk, Perl Meditation|
TIMTOWTDI and other languagesby Ovid (Cardinal)
|on Apr 28, 2002 at 20:35 UTC||Need Help??|
In All roads lead to Rome, I pointed out that different languages lead to different ways to looking at problems. One thing I didn't mention was that different types of languages can lead to different solutions. Ben Tilly's phenomenal post Why I like functional programming was a classic example of how a different type of programming background can lead to a radically different solution. In that node, he attached actions to different sections of text. That is certainly not the way I looked at the problem. Interestingly, I would no longer consider such a horrible solution because continued exposure to different ways of looking at problems leads me to learn better ways of dealing with them. Thus, this post...
Getting back to Rome, the example that I put forward was using nested loops to find all elements in one array that existed in another array. The following code snippets all assume that the two arrays being compared have unique elements.
For small arrays, nested loops are fine, but they don't scale well. Two 10 element arrays require 100 iterations. Two 100 element arrays leads to 10,000 iterations. So, how do you solve the scalability issue? More directly, I am wondering how different types of languages would solve the problem of identifying elements in one array that exist in another. A natural solution in Perl might be to use a hash.
A C programmer, however, might sort one list and perform a binary search. Here's how such a thing might appear if translated to Perl.
Any monks familiar with other languages and styles of programming care to offer other examples? I'd be interested in seeing those in their native language and, if possible, how you would translate those solutions to Perl.
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.