Joachim Breitner

Brainfuck interpreter in Haskell

Published 2006-08-17 in sections English, Haskell.

Two weird languages join. I just wrote a interpreter for Brainfuck programs in Haskell, for the code, see the file bfi.hs. Interesting for Haskell-Newcomers might be how I define the datatype Tape for an infinite band of anything, and then use it as a Tape of Ints for the data, but also a Tape of function for the operators. These Functions take the Machine, the Brainfuck virtual machine so to say, and modify it, thus returning the new machine.

It seems to work pretty well, at least after I found out that my three year old 13-lines Brainfuck interpreter written in perl, bfi.pl, has a bug (it enters a loop at least one time, even if the value is zero at the start).

You can try the interpreter with this code. It takes a string as input and then outputs brainfuck code that itself outputs the input in reverse order:

let

code = "++++++[>+++++++<-]>+[>+>+>+<<<-]>><<< ++++[>>>+++++<<<-]>>>->+++>>,[>,]<[[[<]<<<.>>>>[>]<-]<[<]<.<.>>>[>]<]"

input = "Hello World"

in reverse input == run (run code input) ""

True

Thanks to Mako for introducing me to Brainfuck at DebConf 3

Comments

4:20 is the right time for that post alrighty. There should be some kind of medal really.
#1 Sean am 2006-09-21
Thanks. But nothing beats http://www.nada.kth.se/~matslina/awib/
#2 Joachim Breitner (Homepage) am 2006-09-25
The tape is really cute, indeed. ;-)
#3 Stephan Beyer am 2008-03-21

Have something to say? You can post a comment by sending an e-Mail to me at <mail@joachim-breitner.de>, and I will include it here.