A small programming language

Paul Graham said “Programming languages teach you not to want what they don’t provide.” He meant that as a negative: programmers using less expressive languages don’t know what they’re missing. But you could also take that as a positive: using a simple language can teach you that you don’t need features you thought you needed.

Awk

I read the original awk book recently, published in 1988. It’s a small book for a small language. The language has grown since 1988, especially the Gnu implementation gawk, and yet from the beginning the language had a useful set of features. Most of what has been added since then is of no use to me.

How I use awk

It has been years since I’ve written an awk program that is more than one line. If something would require more than one line of awk, I probably wouldn’t use awk. I’m not morally opposed to writing longer awk programs, but awk’s sweet spot is very short programs typed at the command line.

At one point when I was saying how I like little awk programs, someone suggested I use Perl one-liners instead because then I’d have access to Perl’s much richer set of features, in particular Perl regular expressions. Along those lines, see these notes on how to write Perl one-liners to mimic sed, grep, and awk.

But when I was reading the awk book I thought about how I rarely need the the features awk doesn’t have, not for the way I use awk. If I were writing a large program, not only would I want more features, I’d want a different language.

Now my response to the suggestion to use Perl one-liners would be that the simplicity of awk helps me focus by limiting my options. Awk is a jig. In Paul Graham’s terms, awk teaches me not to want what it doesn’t provide.

Regular expressions

At first I wished awk were more expressive is in its regular expression implementation. But awk’s minimal regex syntax is consistent with the aesthetic of the rest of the language. Awk has managed to maintain its elegant simplicity by resisting calls to add minor conveniences that would complicate the language. The maintainers are right not to add the regex features I miss.

Awk does not support, for example, \d for digits. You have to type [0-9] instead. In exchange for such minor inconveniences you get a simple but adequate regular expression implementation that you could learn quickly. See notes on awk’s regex features here.

The awk book describes regular expressions in four leisurely pages. Perl regular expressions are an order of magnitude more complex, but not an order of magnitude more useful.

 

5 thoughts on “A small programming language

  1. Jakub Kierzkowski

    Well, I was surprised that awk doesn’t support \d.
    I started learning awk recently, after I read Brian W. Kernighan’s “UNIX: A History and a Memoir” (great book, by the way). I had known about awk’s existence for years, but the one one-liner in the book made me fall in love with awk.
    I wanted to write an awk one-liner to solve a Nerdle puzzle one day. \d would fit there, because of one interesting feature of awk: seamless conversion from string to int and back. Fortunately, it’s easy to get a shorthand for digits without \d:
    gawk ‘BEGIN {for (i=0; i <= 9; i++) {x = 1 i 1; d = "[0-9]"; if (x ~ d"8"d) {printf(x "\n");}} exit;}'

  2. I love awk for its simplicity. It was a much better 1st programming language than most when you want to teach kids interested in learning programming. Unfortunately AP Computer Science didn’t pick up on that and instead chose to use C++ followed by Java. AP Computer Science Principles defers to teachers to decide what to use instead, although most teachers have opted to use Python, in some parts.

  3. Jakub Kierzkowski

    Actually, I wanted to paste this (the line I posted before was just a test):

    awk ‘BEGIN {for (i=0; i <= 9; i++) {x = 6 i 8; d = "[0-9]"; if (x/8 ~ "^" d 1 "$") {printf(x/8 "*" 8 "=" x "\n");}} exit;}'

Comments are closed.