Showing posts with label rant. Show all posts
Showing posts with label rant. Show all posts

Thursday, February 5, 2009

XML pattern matching

I've implemented syntax for pattern matching on interpolated XML literals. This is a Scala-inspired feature which may or may not be useful, but is definitely cool-looking. Here's a sample of code:
: dispatch ( xml -- string )
{
{ [ [XML <a><-></a> XML] ] [ "a" prepend ] }
{ [ [XML <b><-></b> XML] ] [ "b" prepend ] }
{ [ [XML <b val='yes'/> XML] ] [ "yes" ] }
{ [ [XML <b val=<->/> XML] ] [ "no" prepend ] }
} switch ;

And here's some examples of what it does:
[XML <a>pple</a> XML] dispatch
=> "apple"
[XML <b>anana</b> XML] dispatch
=> "banana"
[XML <b val="yes"/> XML] dispatch
=> "yes"
[XML <b val="where"/> XML] dispatch
=> "nowhere"

The pattern matching here is based on my inverse library. Hopefully you get the high-level idea of how XML pattern matching works. A caveat is that namespaces are ignored, as in Scala's XML pattern matching, and I haven't thought of a good way to incorporate namespaces.

Jeff Attwood recently wrote a blog post about XML literal syntax*, and how it's a big help. In part, I agree with his statement that it lets you write cleaner code to have XML literals mixed in with everything. That's why I implemented them in Factor.

But I think it's insane to have them built in the way that Scala and, apparently, VB.NET have. Just think if the designers of Java had this idea 15 years ago. They would have made SGML literal syntax rather than XML, and there would be no way to remove this for backwards compatibility concerns. Every implementation of Java would have to contain a full-featured SGML parser. Generating HTML would be easier and allow for very pretty code, though.

XML won't be so prominent forever. It'll always be there, of course, just like SGML is still there in non-XML HTML and DocBook files which refuse to die. But I wouldn't be surprised if, in 20 years, what we want is a literal syntax for something other than XML. We'll still be using a lot of the same programming languages as were invented today, though.

Factor's XML literal syntax and XML pattern matching are just libraries, and I wouldn't want it any other way. Factor's parser needed no adjustments at all to allow for XML literals. If an XML literal is written when the vocabulary xml.literals isn't in scope, it's a syntax error. Sure, you need delimiters [XML XML] around the XML literals, but this is a small price to pay.

In 20 years, if XML isn't useful any longer, then Factor programmers will be just as well off as if they are. If there's a new data format, it'll just be a new library to download, and you'll have literal syntax for that data format. If you were using VB.NET, though, you'd have to upgrade to the most recent version of the language, with a parser that's been vastly complicated.


*He actually talked about HTML literal syntax, but that's such a ridiculous idea that I know he just mistyped. There's no way to tell where an HTML fragment ends, for one, and the HTML DTD would have to be part of the core grammar of the language. You would need delimiters around where the HTML starts and ends, and none of his code fragments have those delimiters. The X key is right next to the letters H and T, so it's an honest typo. His pseudocode sample just before the VB.NET fragment must have also been a simple mistake, as it would seem to imply that either XML literals get printed immediately or that there is some implicit way of collecting them.

Tuesday, March 27, 2007

High school computer science education

I've read so a bunch of rants about why university-level computer science is terrible, but nothing about the high school level. As a high schooler (you read that right), I think this should be given more attention: high school computer science is terrible in at least 90% of schools. I know there's been research as to how to teach children, whether Scheme or Java should be used. But really, that's irrelevant. The relevant thing is, we need better teachers.

Brighton High School is known for being a great school academically. There are four different programs for getting college credit while in high school, including multivariable calculus and linear algebra courses. We ranked in the top 25 in that stupid arbitrary Newsweek survey of high schools, if that means anything. But our computer science is terrible.

It's not for a lack of interest. In 7th and 8th grade, when kids in advanced mathematics started to get graphing calculators (always TI-83s, of course), programming little games became very popular in the primitive BASIC included with the calculator. I wrote a centipede clone and a "racing game", which was more about not hitting the wall than going fast. When high school started, a lot of kids took a computer science elective as freshmen. I decided not to, thinking I wouldn't learn anything. From what I heard from other people, that assessment was accurate. In one semester, meeting every day for nearly an hour, the class learned how to define methods in Java, do simple arithmetic, and at the end, write a program called "bank account" which used two parallel arrays and a simple CLI to let users create a bank account with a name and an amount of money. Using text commands, you could make new bank accounts or deposit or withdraw money. They did this in around 200 lines of code.

Ignoring the fact that, in a more rational programming language, it would be less than 50 lines of code (if that), the real question is, how did they do so little with so much time? It's not that the kids were stupid. It's that the teacher doesn't know anything. The two teachers that have been assigned to teach computers science were both basic math teachers with no real training in software engineering. From what I've heard, they're terribly incompetent, and the only way to actually learn how to program is to learn form the textbook. Algorithms and computational complexity? Data structures more advanced than a primitive array? Those don't come until the "advanced" year-long AP computer science class, and they're only discussed, not implemented. The advanced course was cancelled this year and next year due to lack of demand. Last year, only a couple people did reasonably well on the AP, and these were people who spent class just learning by themselves out of the textbook.

Though it would be nice to use some more abstract language like Scheme or Haskell (or maybe even Factor), that's not really the issue. The issue is that the school doesn't really value computer science as a subject. If they did, there would be a computer science teacher and some thought would go into the curriculum (which is currently just Thinking in Java out of order, with lots and lots of repetition).

In the modern world, computer programming is very important for a large group of people to know. I think it is more important than physics or chemistry because it has a direct use behind it: creating software. I don't have any figures on it, but I'd be surprised if there were more physicists and chemists than there are software engineers. But the core curriculum for students in the United States is homogeneous, fixed and arbitrary: there's English and math (which are good), and then there's a rather nationalistic version of history, and a selection of sciences (physics, biology, chemistry) that is interesting but, for most students, very forgettable. In any case, sciences must be retaken from the beginning at the college level, so high school science study seems almost pointless. By contrast, computer science--like English and math--is often readily applicable if well-taught. I'm not saying it needs to be a mandatory subject, it should just have more attention.

But it's unlikely to get that attention anytime soon as long as it's considered just another elective. For now, I'm doing my best to help my CS-deprived learn more, helping them through SICP and explaining what, exactly, computational complexity is. This is something that is barely covered even in the AP class.

I talked with my principal today about the need for better computer science education in our school. She was nice about it and said she would look into getting more training courses for the computer science teacher, as I suggested. But honestly, I'm not sure if anything will happen.

Update: Hmm, maybe they will listen to me. Later that day, when I wrote this blog post, a different administrator came up to me and said (paraphrased), "The principal told me your ideas about to get classes for computer science through RIT, and I think that's good." So maybe something will happen, even if that's not really what I meant.