Joachim Breitner's Homepage
Brainfuck interpreter in 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
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.