Beginners and Arrays

davorg on 2001-11-13T11:45:51

I've taught beginners before. I know that beginners often get confused by the use of $ to denote single array elements (e.g. $array[0]). I know why they get confused but I think I'm pretty good at getting them to understand it - this is, at least, partly because I think that the rule makes sense.

Yesterday I gave my first beginners course for a while. More importantly it was the the first beginners course I'd given since we've known a lot about the shape of Perl 6.

As always, there was someone who didn't like array (and hash) elements using $. "Oh, I understand exactly why it's there", he said, "but I just don't think it's the most logical way to do it." Of course I disagreed with him and tried to bring him round to my way of thinking. I found this was harder than usual because I now know that in Perl 6 array elements will be accessed using @. It's a lot harder trying to explain away something as logical when you know that it's going to change in the near future.

Have any other trainers come across this problem?


Technology apologists

ziggy on 2001-11-13T15:17:33

I haven't taught Perl to beginners, but I've taught various bits of XML to beginners and intermediate programmers alike.

I've found it to be rather common to need to apologize for the state of the technology being presented. This could be something as simple as using $array[0] vs. @array[0] in Perl, or much more unfortunate like the state of XML. Most recently, I had to teach DTD syntax and usage to a bunch of programmers who were going to be using XML Schema, and had to defend DTDs, only to illustrate the failings of DTDs now that XML Schema exists -- and then to backpedal on XML Schema because it's not widely liked or implemented out in the field yet (and the syntax is kinda backwards as well).

I've heard Python trainers talk about similar issues, like meaningful whitespace. XP presenters also plea to suspend disbelief about pair programming, or spend a lot of time defending pair programming (or test-first programming, or other facets of XP). Thinking back to when I learned C <mumble> years ago, I remember the professor took a stand on learning how CPUs work, as if to apologize for the incredibly low-level nature of the C language.

Re:Technology apologists

Matts on 2001-11-15T09:49:40

Solution: Teach schematron for validating XML instead. Actually, I'd kill for a decent RELAX implementation any day over a Schemas implementation!

You make no apologies and just deal

Elian on 2001-11-13T15:39:08

I find the best thing to do is just lay it out straight. "It seemed to make sense at the time, but it turned out not to be a good idea. Cope. We're fixing this." I can pretty much guarantee it's not the first thing like this your students will have come across... :)

Re:You make no apologies and just deal

ziggy on 2001-11-15T14:29:54

You can make that statement today with a straight face. How about 2 years ago?

Why is $array[0] such a bad thing?

Purdy on 2001-11-14T14:00:14

As I understand it, the funny character demarks the context of the variable. I haven't played around with this (yet), but what if the array was a list of refs to other arrays? Would that be @array[0] or @$array[0]? If the former, then all the more reason the funny character helps, as it denotes the context.

Jason

Re:Why is $array[0] such a bad thing?

Matts on 2001-11-15T09:47:51

Unfortunately it's worse than you think.

@$array[0] binds the @ more tightly than the [0], so it expects $array to be an array ref, which you're de-referencing, rather than $array[0] being the array ref. Except it doesn't work, because as you know, you don't say @x[0], you say $x[0], so when you say @$array[0], it thinks you're dereferencing $array, not @array, and $array doesn't exist (well it might, but it's not what you meant).

Now @{$x[0]} will give you the de-referenced array in the first entry in the @x array. Because here we're being explicit about the precedence using curlies to bind it. But it can get a bit more confusing, because say you wanted the second entry in the first array? i.e. given:

    my @x = ([1,2], [3,4]);

So you want the "2" above. Now that's:

    my $two = @{$x[0]}[1];

But we got a scalar! Now what happened to that "symbol means context" thing here? Screwey, huh?

Fixing this is a Good Thing (tm).

Perl to beginners

clintp on 2001-11-16T14:32:42

I never have a problem with the $/@ distinction in my beginner's classes. I simply grind it into their heads that "$ always means 'one' of something". One element of an array, one item from a hash, and in the case of scalars it's self evident. Keeping the rules simple (at least to begin with), having a "catchphrase" that embodies the rule, and repeating it often works well.

And as for Perl 6... I don't like scaring the novices with evil that hasn't yet come to pass.