http://www.perlmonks.org?node_id=345247


in reply to Re: Re: Re: Perl Internals: Hashes
in thread Perl Internals: Hashes

Well, I learned and programmed in a lot of other languages before I came to Perl so Im aware of the differences. Pascal in particular... *shiver* But anyway, I dont know that I agree with your last point. Personally amongst the first thing I do with a new language is write certain data structure implementations. Try 2-3 trees in VB. Blech. :-) Perl has a natural slant and thus some algorithms work nicely in Perl, others go against the grain as it were and are actually far less efficient than they should be. But aside from that if you learn an algorithm in a language like perl then you probably havent had to deal with as much language and representation specific gunk so you have probably learned the core ideas better. So when you go to implement in another language you're more free of language specific distractions and can concentrate on figuring out how to implement the core behaviour, not the equivelent of the other implementation you wrote.

For instance

sub build_huffman_tree { my $input=shift; my %count; $count{$_}++ for split //,$input; my @list=map { [ $_, $count{$_} ] } keys %count; while (@list>1) { @list=sort {$a->[1] <=> $b->[1]} @list; my ($x,$y)=splice @list,0,2; push @list,[[$x,$y],$x->[1]+$y->[1]]; } return wantarray ? @list : $list[0]; }

The equivelent code in almost any other language would be much larger, and would do no better, and IMO signifigantly worse at illustrating the ideas behind Huffman encoding. Optimising this so that it only calls sort once for instance only detract from the core idea of the algorithm.


---
demerphq

    First they ignore you, then they laugh at you, then they fight you, then you win.
    -- Gandhi