cartercc <> wrote:
> I guess like almost everybody, I like to discuss (argue) the merits of
> different technologies. In my world, the big two are Java and
> ColdFusion. Recently, we had someone with a background in embedded
> systems who has been advocating C. The conversation goes something
> like this:
> him - Does Perl have linked lists?
> me - No.
> him - Therefore, C is better than Perl because it has linked lists.
> me - But Perl has other data structures that are easier to use than
> linked lists.
> him - So what? Perl still doesn't have linked lists.
As I see it it's actually the other way round. C doesn't
"have" linked lists, i.e. there aren't any built-in fea-
tures in the C language especially made for linked lists.
On the other hand in Perl normal arrays can do what makes
linked lists in C so attractive: you can remove and insert
elements at arbitrary places. So, if your collegue says
that C "has" linked lists then you could very well argue
that that's not true and instead Perl "has" linked lists,
just going by a different name, arrays.
In Perl, you don't have to do any book-keeping for getting
the pointers pointing into the right places. And you don't
have to write any special functions for insertion, deleting,
appending etc. Everything can be done with the functions for
dealing with arrays (push/pop/shift/unshift/splice etc.) In-
stead of explicitely iterating over a linked list you can
use map and grep to write things in a single line of code
for which you would need dozends of lines in C with a linked
list. Actually, you mostly need linked lists in C because
those functionalities aren't available.
But if he insists on what he calls linked lists even then
he isn't right. You can create data structures that are
exactly like traditional linked lists in Perl as easily
as you can do it in C. If you do in C
struct node {
Payload_T data
struct node * next;
};
and then create objects of this type ('strcut node') you can
do in Perl exactly the same
my %elem = ( payload => $date,
next => \%some_other_elem );
In C you then would e.g. iterate over the linked list with
for ( l = &list_head; l; l = l->next )
/* do something here for each elements playload l->data */ ;
while in Perl you could do it with
for ( my $l = \%list_head; $l; $l = $l->{ next } )
# do something here for each elements payload $l->{ payload }
;
Not too much of a difference, is there?
See also 'perldoc perlfaq4' and look for "How do I handle
linked lists?".
Being more a C than a Perl programmer I just can say that in C
I use linked lists all of the time. But in nearly ten years of
dabbling with Perl from time to time I never ever had to resort
to using a traditional kind of linked list...
Regards, Jens
--
\ Jens Thoms Toerring ___
\__________________________
http://toerring.de