in reply to Re^3: Perl vs C
in thread Perl vs C

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:

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++.)

Replies are listed 'Best First'.
Re^5: Perl vs C
by Marshall (Canon) on Mar 14, 2009 at 19:58 UTC
    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.

      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.

      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.

        I think the main point here is that in Perl, an variable like: @array is very different than a C, Basic, Fortran or what ever array. The Perl list is very much like a linked list, but not quite. My analogy wasn't great. I don't know of a language that has a thing like a Perl list. You can push things onto the end of a Perl list, pop things from the bottom (like LIFO queue), even do the same things at the "top" of the list - hey that's what the common "shift" does!. Also it is possible to insert things in the middle, take things out of the middle. None of of those things are possible with a traditional simple "array", but are common with linked lists in C. A Perl list has all these properties and it can be sorted easily! Whoa!

        In Perl I would call an "array" of lists, a List of Lists, (LoL). This is a list of references to other lists.

        In C the most flexible representation of a 2-d numeric matrix is an array of pointer to arrays of ints. In Perl this is just a LoL, List of List.

        Since a Perl list has fundamentally different properties from a traditional "array" in other languages, I see no reason not to call this a "list".

          A reply falls below the community's threshold of quality. You may see it by logging in.