A random mental walk.

Thursday, October 11, 2007

Hello Worlf!/Brainf*ck

Students are training for an upcoming ACM programming contest by tackling problems from previous contests. One of the examples from the November 2005 ACM Regional Collegiate Programming Contest at Kean University was the "ungodly creation of Urban Muller", a computer programming language which, in the interest of propriety, is usually written "brainf*ck".

<digression>
First programs written in a new programming language are often referred to as "Hello World" programs in honor of the first program in the classic book, "The C Programming Language" by Kernighan and Richie. Hello World programs display the words "Hello World!" on the screen or computer console.

All the problems in ACM competitions are formatted the same way and each problem contains sample inputs and the corresponding program outputs so competitors can verify that their programs are working correctly.

It's been a while since I programmed, but I tackled the brainf*ck problem with a classic "top down" approach using the C programming language. (C is usually not the programming language of choice because the language let's programmers do horrible things, often without even a warning. Of course that's the attraction of the language, but I'll limit my digression.) If I were more current I'd be using C++ or Java with object-oriented techniques, but C, with it's cavalier treatment of characters and integers actually was an advantage in writing this particular program. (y ='b' +2; is a perfectly legal statement in C.)

Using top down programming I started with the big picture and worked down toward the actual nitty-gritty of creating the guts of the program. I was surprised at how smoothly things went. Of course, I had advantages over those in the actual competition: I was using Microsoft's Visual Studio with it's exceptional debugger, help just the press of a function key away, and, of course, I wasn't under time pressure. (This endorsement of an older development system has not been influenced by Microsoft, Microsoft partners or affiliates, or Steve Ballmer's threat to sue my pants off if I did not endorse a Microsoft product.)

In actual competition students are using vi for editing, the man pages for help/reference, and dbx on Linux for debugging, all in all a much more difficult environment. (vi is a text editor best described as Trivial Pursuit, Keyboard edition.)
</digression>

To give you an idea of how weird brainf*ck appears, the following program prints out the Roman alphabet in capital letters:



+ + + + + +++++++++++++++++++++>
++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++
+< [ >.+<- ]
end



Because of my approach, I wouldn't be able to see how the program displayed results until the whole program was written. (This is not technically true, but in the interest of blogging, please accept the statement as true.) So imagine my delight as I got to see the first output of:


<-]>.<+++++[>++++++<-]>-.+++++++..
+++.<++++++++[>>++++<<-]>>.<<++++[>
------<-]>.<++++[>++++++<-]>.+++.
------.—------.>+.
end



Using the debugger to step through the program I got 'H' followed by 'e', then 'l', another 'l'. I was thrilled. Throwing caution to the wind, I let it rip and got:
Hello Worlf!

Sigh. The other sample inputs worked fine though. I'll go back and see what I did wrong. It was so tantalizing to feel that the very first run through of program would work flawlessly.

I think I've taken my run-in with my fallibility in stride. I've started to refer to first programs as "Hello Worlf" programs.

No comments:

Blog Archive