Re: Arrays vs. Lists
--- Michael Lazzaro [EMAIL PROTECTED] wrote: I'm trying, and failing, to accurately and definitively answer the question what's the difference between an array and a list in Perl6? If someone can come up with a simple but accurate definition, it would be helpful. How's this? A number is a literal (e.g., 3) that can be used as the initializer for a scalar. A string is a literal (e.g., Hello, world) that can be used as the initializer for a scalar. A list is a literal (e.g., '(3, Hello, world)') that can be used as the initializer for an array. With one exception, places in perl that require a scalar can be given a literal number or string. Likewise, places in perl that require an array can be given a list. The exception is lvalues -- you can't say 3 = Hello, world; -- the left-hand side of an assignment operation requires an assignable thing, not a literal. So the difference between a list and an array is one of assignability - a list can be indexed, examined, copied, iterated over using for, etc. But in order to make changes you have to have an array -- a container for a list. Because arrays can do all the things above, plus shift, pop, append, delete, etc. ==? =Austin
Re: Arrays vs. Lists
On 2003-02-07 at 11:13:07, Austin Hastings wrote: --- Michael Lazzaro [EMAIL PROTECTED] wrote: I'm trying, and failing, to accurately and definitively answer the question what's the difference between an array and a list in Perl6? How's this? A list is a literal (e.g., '(3, Hello, world)') that can be used as the initializer for an array. [...] places in perl that require an array can be given a list. The exception is lvalues -- you can't say 3 = Hello, world; -- the left-hand side of an assignment operation requires an assignable thing, not a literal. So the difference between a list and an array is one of assignability. Not really, though. A list can be an lvalue, provided it is a list of lvalues: ($a, $b, $c) = 1,2,3; Although this may reasonably be regarded as a special case; you certainly can't pop a list: (1,2,3).pop = error But there's also the case of anonymous arrays, constructed through reference via [ . . . ]. These are pop'able: [1,2,3].pop = 3 But they certainly aren't lvalues: [$a,$b,$c] = 1,2,3 = error Unless some magic autoconversion happens. -- Mark REED| CNN Internet Technology 1 CNN Center Rm SW0831G | [EMAIL PROTECTED] Atlanta, GA 30348 USA | +1 404 827 4754
Re: Arrays vs. Lists
On 2003-02-07 at 14:26:42, Mark J. Reed wrote: Not really, though. A list can be an lvalue, provided it is a list of lvalues: ($a, $b, $c) = 1,2,3; Forgot the parens on the right side, there: ($a, $b, $c) = (1,2,3); But they certainly aren't lvalues: [$a,$b,$c] = 1,2,3; = error [$a, $b, $c] = (1,2,3) = still an error Just to flesh it out: [$a, $b, $c] = [1,2,3] = still an error ($a, $b, $c) = [1,2,3] = not an error; $a is [1,2,3], $b and $c undef. -- Mark REED| CNN Internet Technology 1 CNN Center Rm SW0831G | [EMAIL PROTECTED] Atlanta, GA 30348 USA | +1 404 827 4754
Re: Arrays vs. Lists
--- Mark J. Reed [EMAIL PROTECTED] wrote: On 2003-02-07 at 11:13:07, Austin Hastings wrote: --- Michael Lazzaro [EMAIL PROTECTED] wrote: I'm trying, and failing, to accurately and definitively answer the question what's the difference between an array and a list in Perl6? How's this? A list is a literal (e.g., '(3, Hello, world)') that can be used as the initializer for an array. [...] places in perl that require an array can be given a list. The exception is lvalues -- you can't say 3 = Hello, world; -- the left-hand side of an assignment operation requires an assignable thing, not a literal. So the difference between a list and an array is one of assignability. Not really, though. A list can be an lvalue, provided it is a list of lvalues: ($a, $b, $c) = 1,2,3; Hmm. You're kind of weaseling there because that's DWIM magic for 3 lines of code, but I don't know how to get there. Although this may reasonably be regarded as a special case; you certainly can't pop a list: (1,2,3).pop = error But could you do it the other way (function instead of method)? pop (1,2,3) = ? But there's also the case of anonymous arrays, constructed through reference via [ . . . ]. These are pop'able: [1,2,3].pop = 3 But they certainly aren't lvalues: [$a,$b,$c] = 1,2,3 = error Actually, they're literal array references, not arrays. I'm unsure how the mechanics are going to act in p6, since we're hiding the - on refs. But in my heart of (c coding) hearts, it's a pointer. =Austin
Re: Arrays vs. Lists
On 2003-02-07 at 12:18:21, Austin Hastings wrote: Although this may reasonably be regarded as a special case; you certainly can't pop a list: (1,2,3).pop = error But could you do it the other way (function instead of method)? pop (1,2,3) = ? Nope. At least, not in Perl 5: Type of arg 1 to pop must be array (not list) But there's also the case of anonymous arrays, constructed through reference via [ . . . ]. These are pop'able: [1,2,3].pop = 3 But they certainly aren't lvalues: [$a,$b,$c] = 1,2,3 = error Actually, they're literal array references, not arrays. You can't have an array reference without an array; the reference has to refer to something. :) The referred-to-array in this case has no name, hence anonymous arrays, constructed through reference. I'm unsure how the mechanics are going to act in p6, since we're hiding the - on refs. But in my heart of (c coding) hearts, it's a pointer. A reference is fundamentally a pointer, but that doesn't help. My point was that if you're talking about lists vs. arrays, you have at least three different syntaxes to distinguish: (1,2,3) @arrayName [1,2,3] These all do different things, and autoconversion just adds to the confusion - for instance, @arrayName is normally an array, but in certain contexts it will be automatically turned into a reference ($aRef = @arrayName) or flattened into a list (print @arrayName). -- Mark REED| CNN Internet Technology 1 CNN Center Rm SW0831G | [EMAIL PROTECTED] Atlanta, GA 30348 USA | +1 404 827 4754
Re: Arrays vs. Lists
MJR == Mark J Reed [EMAIL PROTECTED] writes: MJR A reference is fundamentally a pointer, but that doesn't help. My point MJR was that if you're talking about lists vs. arrays, you have at least MJR three different syntaxes to distinguish: MJR (1,2,3) MJR @arrayName MJR [1,2,3] one simple explanation still works i think. arrays are allocated and lists are on the stack. so arrays can have references to them but lists can't. this works with both lvalue and rvalue. a list of lvalues is on the stack and can be assigned to. you can't push/pop/splice a list on the stack. you can take slices from a list on the stack. the whole notion is that lists are always temporary and arrays can be as permanent as you want (an array ref going quickly out of scope is very temporary). lists can't live beyond the current expression but arrays can. can anyone see any changes in perl6 to invalidate that separation of lists and arrays? uri -- Uri Guttman -- [EMAIL PROTECTED] http://www.stemsystems.com - Stem and Perl Development, Systems Architecture, Design and Coding Search or Offer Perl Jobs http://jobs.perl.org Damian Conway Perl Classes - January 2003 -- http://www.stemsystems.com/class
Re: Arrays vs. Lists
On Friday, February 7, 2003, at 02:07 PM, Uri Guttman wrote: the whole notion is that lists are always temporary and arrays can be as permanent as you want (an array ref going quickly out of scope is very temporary). lists can't live beyond the current expression but arrays can. Along those lines, the closest I've been able to come so far to a usable two-sentence definition is: -- A list is an ordered set of scalar values. -- An array is an object that stores a list. But I'm not sure that holds water. MikeL
Re: Arrays vs. Lists
ML == Michael Lazzaro [EMAIL PROTECTED] writes: ML On Friday, February 7, 2003, at 02:07 PM, Uri Guttman wrote: the whole notion is that lists are always temporary and arrays can be as permanent as you want (an array ref going quickly out of scope is very temporary). lists can't live beyond the current expression but arrays can. ML Along those lines, the closest I've been able to come so far to a ML usable two-sentence definition is: ML -- A list is an ordered set of scalar values. ML -- An array is an object that stores a list. but you can't derive the rules about allowing push/pop/splice/slice from that pair of defintions. you can simplify my pair to: a list is temporary ordered set of scalar values that lives only in a single expression an array is an ordered set of scalar values that is allocated and can live between expressions. note that i said expression and not statement. you can't have the same list in two parts of an expression while you can with an array (ref or plain). that implies you can't change a list since it only exists once. another (and shorter pair) is this: (note that this is from the whole list point of view, not its elements) lists are read only arrays are read/write that allows slices on lists but not push/pop/splice. the lvalueness of their elements doesn't matter. the two sets of pairs above can be combined for clarity: (again these are from the whole list/array point of view) a list lives in a single place in a single expression and can't be modified. an array can live in multiple places in multiple expressions and can be changed the single place makes it impossible to take a ref to a list. the multiple places for an array implies references are possible. the array can be changed since it has state that will store the change. a list has no such state as it will die when the expression is done. uri -- Uri Guttman -- [EMAIL PROTECTED] http://www.stemsystems.com - Stem and Perl Development, Systems Architecture, Design and Coding Search or Offer Perl Jobs http://jobs.perl.org Damian Conway Perl Classes - January 2003 -- http://www.stemsystems.com/class
Re: Arrays vs. Lists
Michael Lazzaro [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... Along those lines, the closest I've been able to come so far to a usable two-sentence definition is: -- A list is an ordered set of scalar values. quibble: that's an ordered bag, isn't it? ;) -- An array is an object that stores a list. My phrasing of the distinction is that a list is a lexical entity, whilst an array is a variable. Anonymous array constructors are just special syntax for passing a list to an array (or Array) constructor.
Re: Arrays vs. Lists
On Fri, Feb 07, 2003 at 02:30:47PM -0500, Mark J. Reed wrote: On 2003-02-07 at 14:26:42, Mark J. Reed wrote: Not really, though. A list can be an lvalue, provided it is a list of lvalues: Note that to avoid the burden of writing an explicit slice, 'undef' is considered as a lvalue in such a context. I see no reason for that behavior to change in perl6: ($a, undef, $b) = (1, 2, 3); # equivalent to ($a,$b) = (1, 3) Note this is only true of undef. You can't stick any literal in its splace. ($a,1,$b) = qw(1,2,3) Can't modify constant item in list assignment at (eval 5)[/usr/lib/perl5/5.8.0/perl5db.pl:17] line 2, at EOF -- stef
Re: Arrays vs. Lists
ML == Michael Lazzaro [EMAIL PROTECTED] writes: ML On Friday, February 7, 2003, at 03:38 PM, Uri Guttman wrote: but you can't derive the rules about allowing push/pop/splice/slice from that pair of defintions. ML Is there any syntactic reason why both of the following cannot be ML allowed? ML (1,2,3).pop that is no different than saying (3). as the list can't be modified nor a ref taken, the pop is illegal. ML [1,2,3].pop ML I don't know that one is any more/less useful than the other, and it ML would seem a list could be silently promoted to an array where it is ML used as an array. For example, ML \(1,2,3) ML returns an array reference... in perl5 it returns a list of refs ( \1, \2, \3 ). i dunno the perl6 semantics. it could be the same as [ 1, 2, 3 ] which means it is not a list but sugar for a new anon array and more like: do{ \my @foo = ( 1, 2, 3 ) } but we only need [] for all that. uri -- Uri Guttman -- [EMAIL PROTECTED] http://www.stemsystems.com - Stem and Perl Development, Systems Architecture, Design and Coding Search or Offer Perl Jobs http://jobs.perl.org Damian Conway Perl Classes - January 2003 -- http://www.stemsystems.com/class
Re: Arrays vs. Lists
On Fri, Feb 07, 2003 at 06:38:36PM -0500, Uri Guttman wrote: ML == Michael Lazzaro [EMAIL PROTECTED] writes: ML Along those lines, the closest I've been able to come so far to a ML usable two-sentence definition is: ML -- A list is an ordered set of scalar values. ML -- An array is an object that stores a list. but you can't derive the rules about allowing push/pop/splice/slice from that pair of defintions. 1) A list is an ordered grouping of scalar values. 2) An array is an object that stores a list. 3) Assignment and splices can be performed on both lists and arrays. 4) Operators like push/pop/splice/shift/unshift operate only on arrays. lists are read only Not quite: ($a, $b, $c) = 1..3; Z.
Re: Arrays vs. Lists
Date: Fri, 7 Feb 2003 14:46:37 -0800 From: Michael Lazzaro [EMAIL PROTECTED] On Friday, February 7, 2003, at 02:07 PM, Uri Guttman wrote: the whole notion is that lists are always temporary and arrays can be as permanent as you want (an array ref going quickly out of scope is very temporary). lists can't live beyond the current expression but arrays can. Along those lines, the closest I've been able to come so far to a usable two-sentence definition is: -- A list is an ordered set of scalar values. -- An array is an object that stores a list. But I'm not sure that holds water. Rather, -- An array is a variable. -- A list is a value. It's just a special kind of value, that distributes certain operators over its elements. It's still a value. The discrepancy about Array's methods is simple. Can you Cchop a string literal? That's why you can't Cpop a list. Luke
Re: Arrays vs. Lists
On Friday, February 7, 2003, at 04:24 PM, Uri Guttman wrote: ML \(1,2,3) ML returns an array reference... in perl5 it returns a list of refs ( \1, \2, \3 ). i dunno the perl6 semantics. it could be the same as [ 1, 2, 3 ] which means it is not a Sorry, I was misremembering a thread. I remember (vaguely) now... can't do what I suggested because it's something like \($x) should never be a list ref, which means we would have to treat parens differently depending on how many things are inside them, etc, which pointedly won't work. If someone remembers when that damn thread happened, or better still remembers the outcome (if any), drop me a pointer? MikeL
Re: Arrays vs. Lists
AT == Adam Turoff [EMAIL PROTECTED] writes: AT On Fri, Feb 07, 2003 at 06:38:36PM -0500, Uri Guttman wrote: ML == Michael Lazzaro [EMAIL PROTECTED] writes: ML Along those lines, the closest I've been able to come so far to a ML usable two-sentence definition is: ML -- A list is an ordered set of scalar values. ML -- An array is an object that stores a list. but you can't derive the rules about allowing push/pop/splice/slice from that pair of defintions. AT 1) A list is an ordered grouping of scalar values. AT 2) An array is an object that stores a list. AT 3) Assignment and splices can be performed on both lists and arrays. you can't assign to a list. you can assign to lvalues in a list. the list doesn't change. it is a list of lvalues before and after the assignment. AT 4) Operators like push/pop/splice/shift/unshift operate only on arrays. lists are read only AT Not quite: ($a, $b, $c) = 1..3; that list is still unmodified, same size, no elements are changed. the elements are lvalues which have their values changed, but the list itself is still read only. only my two definitions are needed, not 4. simpler is better. :) uri -- Uri Guttman -- [EMAIL PROTECTED] http://www.stemsystems.com - Stem and Perl Development, Systems Architecture, Design and Coding Search or Offer Perl Jobs http://jobs.perl.org Damian Conway Perl Classes - January 2003 -- http://www.stemsystems.com/class