Re^4: Perl vs C
by ikegami (Patriarch) on Mar 14, 2009 at 17:41 UTC
|
Please explain what you think the difference is between a Perl array and a Perl List.
When someone talks of lists in Perl, they are usually talking about one of the following:
- A list of values on the stack.
- The n-ary operator which creates a list on the stack. (e.g. "EXPR, EXPR, EXPR")
There is also "list context", a context in which expressions can be evaluated.
In contrast, an array is a type of variable. There is no list variable type.
I'd be hard pressed to find similarities between list values, list operators, list context and array variables because values, operators, contexts and variables are fundamentally different from each other. It's not just nomenclature.
I am still not "getting it". A simple Perl list is similar to a C char ** array
A Perl array is similar to T* array in C, except it automatically resizes itself when needed (like C++'s std::vector). Due to C's type system, C doesn't have anything similar to Perl lists. (Same goes for C++.)
| [reply] [d/l] [select] |
|
|
In contrast, an array is a type of variable. There is no list variable type.
I would agree with this.
@xyz= (1,2,3);
xyz is the name of an array with a list of: 1,2,3. But the problems is, is that this confuses folks. I often simply say that xyz is a list.
| [reply] |
|
|
But the problems is, is that this confuses folks.
That's because a list is not a first-class data structure in Perl. There's a perfectly good and accurate name for the first-class indexed aggregate data structure in Perl. That name is "array". A list is something different. If you conflate the two, you will confuse people. They will start to think that parentheses create lists instead of grouping syntactic elements to disambiguate operator precedence. They may believe that operators which operate on lists will operate only on arrays, and that those operators do not introduce list context to their operands.
Sloppy terminology produces sloppy understanding.
| [reply] |
|
|
1,2,3 is a list literal aka list operator. It may evaluate to a list of its items or to its last item.
@xyz is an array. As an operator, it may evaluate to a list of its elements or to its number of elements.
But the problems is, is that this confuses folks.
That's to be expected. "An array with a list" makes no sense. Arrays have elements, not lists.
How is the incorrect "@xyz is a list" any simpler than the correct "@xyz is an array"? "List" is already overloaded enough. You're doing yourself and your readers a disservice by calling arrays lists.
| [reply] [d/l] [select] |
|
|
|
|
|
Re^4: Perl vs C
by JavaFan (Canon) on Mar 15, 2009 at 11:50 UTC
|
Please explain what you think the difference is between a Perl array and a Perl List.
Arrays are variables, lists are values. It's the same difference as between $foo and 3.
I think this just some kind of nomenclature difference.. Not any real disagreement!
Differences in nomenclature are not something to trivialize - specially not when it's about important concepts.
A simple Perl list is similar to a C char ** array (array of pointers of pointers to strings)
No, it's not. First of all, a char is not a string, but a (small) integer. Depending on usage, a C char is equivalent to a Perl integer, or a Perl string consisting of exactly one character. Secondly, in C, array elements all have the same size - all elements are integers, characters or pointers for instance. (Pointers in your example). In Perl, values of a list can have different "sizes" - or rather, different types (mixed integers, references, objects, strings, etc) - as the "size" of a value isn't a useful concept in Perl (it is in perl, but not in Perl). Third, and this is the big one, elements of an array may change (both in C and in Perl). But in Perl, lists are unmutable (of course they can be changed by perl, or by using XS - but not from a Perl POV). They are as unmutable as the value 3.
| [reply] [d/l] [select] |
|
|
#!usr/bin/perl -w
use warnings;
my @list = ("a1", "b23", "c45", "d1");
print join (" ",@list),"\n";
#prints: a1 b23 c45 d1
In Perl, the list "knows" how big it is.
This is not true in C! In C we have to do stuff to help things out! Like this:
include <stdio.h>
int main ()
{
char *list[] = {"a1", "b23", "c45", "d1",'\0'};
// list is an array of pointers to chars
char **mover;
for (mover=list; *mover ;mover++)
printf ("%s ",*mover);
printf("\n");
}
// prints: a1 b23 c45 d1
For me, the difference is obvious! The Perl version is cool!
The alternative way in C would be to keep track of the number of items in the list array instead of a NULL value sentinel (and yes, there are more ways than than one I showed above). All ways are a pain in the rear compared with Perl!
Perl lists may be changed at will just like in C. I don't understand this comment: "lists are unmutable". Please explain, I just don't understand what you mean. | [reply] [d/l] [select] |
|
|
I don't understand this comment: "lists are unmutable". Please explain, I just don't understand what you mean.
Try to make pure Perl program that modifies a list. Come back when you either have such a program, or when you are convince this isn't possible.
| [reply] |
|
|
|
|
|