http://qs1969.pair.com?node_id=162711

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.

#!/usr/bin/perl -w use strict; my @alphas = qw/ 9 4 3 2 22 13 7 140 95 278/; my @betas = qw/ 8 3 4 1 278 94 15 7 19 200/; my @results; foreach my $alpha ( @alphas ) { foreach my $beta ( @betas ) { if ( $alpha == $beta ) { push @results, $beta; } } } @results = sort { $a <=> $b } @results; print "Linear search: @results\n";

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.

my @alphas = qw/ 9 4 3 2 22 13 7 140 95 278/; my @betas = qw/ 8 3 4 1 278 94 15 7 19 200/; my @results; my %alpha; @alpha{ @alphas } = undef; foreach my $beta ( @betas ) { if ( exists $alpha{ $beta } ) { push @results, $beta; } } @results = sort { $a <=> $b } @results; print "Hash lookup: @results\n";

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.

my @alphas = qw/ 9 4 3 2 22 13 7 140 95 278/; my @betas = qw/ 8 3 4 1 278 94 15 7 19 200/; my @results; my @sorted = sort { $a <=> $b } @alphas; foreach my $beta ( @betas ) { my $index = binary_search( \@sorted, $beta ); if ( defined $index ) { push @results, $beta; } } @results = sort { $a <=> $b } @results; print "Binary search: @results\n"; sub binary_search { my ( $array, $target ) = @_; my ( $low, $high ) = ( 0, scalar @$array ); while ( $low < $high ) { use integer; my $current = ( $low + $high ) / 2; if ( $array->[$current] < $target ) { $low = $current + 1; } else { $high = $current; } } if ( $low < @$array and $array->[$low] == $target ) { return $low; } else { return undef; } }

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.

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.