<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Programming in the 21st Century</title>
<link rel="self" href="http://prog21.dadgum.com/atom.xml"/>
<link rel="alternate" href="http://prog21.dadgum.com/"/>
<id>http://prog21.dadgum.com/</id>
<updated>2013-05-11T00:00:00-06:00</updated>
<entry>
<title>A Short Quiz About Language Design</title>
<link rel="alternate" type="text/html" href="http://prog21.dadgum.com/172.html"/>
<id>http://prog21.dadgum.com/172.html</id>
<published>2013-05-11T00:00:00-06:00</published>
<updated>2013-05-11T00:00:00-06:00</updated>
<author><name>James Hague</name></author>
<content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml">Suppose you're designing a programming language. What syntax would you use for a string constant? This isn't a trick; it's as simple as that. If you want to print <code>Hello World</code> then how do you specify a basic string like that in your language?
<br/><br/>I'll give you a moment to think about it.
<br/><br/>The obvious solution is to use quotes: <code>"Hello World"</code>. After all, that's how it works in English, so it's easy to explain to new students of your language. But then someone is going to ask "What if I want to put a quotation mark <i>inside</i> a string? That's a legitimate question, because it's easy to imagine displaying a string like:
<pre>"April 2013" could not be found.
</pre>There are a couple of options to fix this. Some form of escape character is one, so an embedded quote is preceded by, say, a backslash. That works, but now you've got to explain a second concept in order to explain strings. Another option is to allow both single and double quotes. If your string contains single quotes, enclose it in double quotes, and vice-versa. A hand goes up, and someone asks about how to enter this string:
<pre>"April 2013" can't be found.
</pre>Ugh. Now you have two kinds of string delimiters, and you still need escapes. You need to explain these special cases up front, because they're so easy to hit.
<br/><br/>What if instead falling back on the unwritten rule of using single and double quotes, strings were demarcated by something less traditional? Something that's not common in Latin-derived languages? I'll suggest a vertical bar:
<pre>|"April 2013" can't be found.|
</pre>That may be uncomfortable at first glance, but give it a moment. Sure, a vertical bar will end up in a string at some point--regular expressions with alternation come to mind--but the exceptional cases are no longer blatant and nagging, and you could get through a beginning class without even mentioning them.
<br/><br/>(If you liked this, you might enjoy <a href="http://prog21.dadgum.com/73.html">Explaining Functional Programming to Eight-Year-Olds</a>.)
</div></content>
</entry>
<entry>
<title>Remembering a Revolution That Never Happened</title>
<link rel="alternate" type="text/html" href="http://prog21.dadgum.com/171.html"/>
<id>http://prog21.dadgum.com/171.html</id>
<published>2013-04-21T00:00:00-06:00</published>
<updated>2013-04-21T00:00:00-06:00</updated>
<author><name>James Hague</name></author>
<content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml">Twenty-three years ago, a book by Edward Cohen called <a href="http://www.amazon.com/Programming-1990s-Introduction-Calculation-Monographs/dp/0387973826">Programming in the 1990s: An Introduction to the Calculation of Programs</a> was published. It was a glimpse into the sparkling software development world of the future, a time when ad hoc coding would be supplanted by Dijkstra-inspired manipulation of proofs. Heck, no need to even run the resulting programs, because they're right by design.
<br/><br/>Clearly Mr. Cohen's vision did not come to pass, but I co-opted the title for this blog.
<br/><br/>That book is a difficult read. It starts out as bright-eyed and enthusiastic as you could expect a computer science text to be, then rapidly turns into chapter-long slogs to prove the equivalent of a simple linear search correct. It wasn't the difficulty that made the program derivation approach unworkable. Reading and writing music looks extraordinarily complex and clunky to the uninitiated, but that's not stopping vast numbers of people from doing so. The problem is that for almost any non-trivial program, it's not clear what "correct" means.
<br/><br/>Here's a simple bit of code to write: display a sorted list of the filenames in a folder. That should take a couple of minutes, including googling around for how to get the contents of a directory.
<br/><br/>Except that on some systems you're getting weird filenames like "." and ".." that you don't want to display.
<br/><br/>Except that there are also hidden files, either based on an attribute or a naming convention, and you should ignore those too.
<br/><br/>Except that you need the sort to be case insensitive or else the results won't make sense to most users.
<br/><br/>Except that some people are using spaces between words and some are using underscores, so they should be treated the same when sorting.
<br/><br/>Except that a naive sort is going to put "File 10" before "File 9", and while that's logical in the cold innards of the CPU, it's no excuse to present the data that way.
<br/><br/>And this is a well-understood, weird old relic of a problem that's <i>nothing</i> compared to all the special cases and exceptions needed to implement a solid user experience in a modern app. Making beautiful code ugly--and maybe impossible to prove correct--by making things easier for the user is a good thing.
<br/><br/>(If you liked this, you might enjoy <a href="http://prog21.dadgum.com/87.html">Write Code Like You Just Learned How to Program</a>.)
</div></content>
</entry>
<entry>
<title>Exploring the Lower Depths of Terseness</title>
<link rel="alternate" type="text/html" href="http://prog21.dadgum.com/170.html"/>
<id>http://prog21.dadgum.com/170.html</id>
<published>2013-04-08T00:00:00-06:00</published>
<updated>2013-04-08T00:00:00-06:00</updated>
<author><name>James Hague</name></author>
<content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml">There's a 100+ year old system for recording everything that happens in a baseball game. It uses sheet of paper with a small box for each batter. Whether that batter gets to base or is out--and why--gets coded into that box. It's a scorekeeping method that's still in use at the professional and amateur level, and at major league games you can buy a program which includes a scorecard.
<br/><br/>What's surprising is how cryptic the commonly used system is. For starters, each position is identified by a number. The pitcher is 1. The center fielder 8. If the ball is hit to the shortstop who throws it to the first baseman, the sequence is 6-3. See, there isn't even the obvious mnemonic of the first, second, and third basemen being numbered 1 through 3 (they're 3, 4, and 5).
<br/><br/>In programming, no one would stand for this. It breaks the rule of not having magic numbers. I expect the center fielder would be represented by something like:
<pre>visitingTeam.outfield.center
</pre>The difference, though, is that programming isn't done in real-time like scorekeeping. After the initial learning curve, 8 is much more concise, and the terseness is a virtue when recording plays with the ball moving between multiple players. Are we too quick to dismiss extremely terse syntax and justify long-winded notations because they're easier for the uninitiated to read?
<br/><br/>Suppose you have a file where each line starts with a number in parentheses, like "(124)", and you want to replace that number with an asterisk. In the vim editor the keystrokes for this are "<code>^cib*</code>" followed by the escape key. "^" moves to the start of the line. The "c" means you're going to change something, but what? The following "ib" means "inner block" or roughly "whatever is inside parentheses." The asterisk fills in the new character.
<br/><br/>Once you get over the dense notation, you may notice a significant win: this manipulation of text in vim can be described and shared with others using only five characters. There's no "now press control+home" narrative.
<br/><br/>The ultimate in terse programming languages is J. The boring old "*" symbol not only multiplies two numbers, but it pairwise multiplies two lists together (as if a map operation were built in) and also multiplies a scalar value with each element of a list, depending on the types of its operands. 
<br/><br/>That's what happens with two operands anyway. Each verb (the J terminology for "operator"), also works in a unary fashion, much like the minus sign in C represents both subtraction and negation. When applied to a lone value "*" is the sign function, returning either -1, 0, or 1 if the operand is negative, zero, or positive.
<br/><br/>So now each single-character verb has two meanings, but it goes further than that. To increase the number of symbolic verbs, each can have either a period or a colon as a second character, and then each of these have both one and two operand versions. "*:" squares a single parameter or returns the nand ("not and") of two parameters. Then there's the two operand version of "*." which computes the least common multiple, and I'll give it up now before everyone stops reading.
<br/><br/>Here's the reason for this madness: it allows a wide range of built-in verbs that never conflict with user-defined, alphanumeric identifiers. Without referencing a single library you've got access to prime number generation ("p:"), factorial ("!"), random numbers ("?"), and matrix inverse ("%.").
<br/><br/>Am I recommending that you switch to vim for text editing and J for coding? No. But when you see an expert working with those tools, getting results with fewer keystrokes than it would take to import a Python module, let alone the equivalent scripting, then...well, there's something to the terseness that's worth remembering. It's too impressive to ignore simply because it doesn't line up with the prevailing aesthetic for readable code.
<br/><br/>(If you liked this, you might enjoy <a href="http://prog21.dadgum.com/114.html">Papers from the Lost Culture of Array Languages</a>.)
</div></content>
</entry>
</feed>