Two Guys Arguing

pathological programming

Posted in bf by youngnh on 04.10.09

My post count is slipping.  I’m too worried about writing long, polished entries with a point to them, when what I’m generally working on is short, scatterbrained and only interesting to me for short periods of time.  So, who wants to ride bikes?

The shiny object of my current attention is the brainfuck language.  That’s actually its name, but I’ll be referring to it in polite company as bf.  A few weeks ago, while lamenting to myself the fact that I haven’t entered a horse in the Lambda Lounge Shootout race, I decided that its far too easy to write readable code in a well-established, if not widely used language.  Nevermind that this was the entire spirit of the shootout — to expose developers to how real software is written in other languages — I decided that I was going to attempt to write it in a pathological language.

How pathological?  bf is based around a 30,000 element array and 8 operators.

+ – > < . , [ ]

+ increments the value of the current array element
- decrements the value of the current arrayelement
> increments the current array index pointer
< decrements the current array index pointer
. prints out the value of the current array element
, stores one character of input in the current array element
[ marks the start of a loop
] marks the end of a loop

Loops can be arbitrarily nested and start execution of their bodies if the current array element is zero. When the ending ] is reached, the execution will loop only if the current array element is non-zero.

That’s it.  No strings, no if statements, no methods, no threads, no concurrency, no objects.  It has zero of the features that language presentations at Lambda Lounge have focused on. The language is Turing complete though, so theoretically, any computable function can be expressed in bf.  I intend to put that assumption to the test this coming month.

About these ads

5 Responses

Subscribe to comments with RSS.

  1. zac said, on 04.10.09 at 12:12 pm

    Brainfuck’s nifty, but it’s child, Ook is even sweeter.

    • youngnh said, on 04.10.09 at 1:27 pm

      Ook is great because it was designed _for_ Orangutangs. Its operators correspond one-to-one to bf’s, but I love the looping operators the most. With Ook! Ook? ( ['s equivalent) and Ook? Ook! ( ]‘s counterpart) you can almost understand what it does, “Do this! Uh, if its zero?”…”Done?, Do it again!”

  2. benjaminplee said, on 04.10.09 at 12:34 pm

    I had heard of bf before but never knew anything about it. I may have to try and put together a few simple programs just to see if I can. Reminds me of my college days and Finite Automata Theory.

  3. Alex Miller said, on 04.14.09 at 8:19 pm

    This is awesome…please stick it out if at all possible!

  4. [...] Awesome – Nate Young is trying to implement the vending machine for Lambda Lounge in brainf*ck [...]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.