Swirly Mein Kopf

Thursday, August 17. 2006

Brainfuck interpreter in Haskell

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

Trackbacks


No Trackbacks

Comments

Display comments as (Linear | Threaded)

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

Add Comment



To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

What is the first name of the owner of this blog? / Wie heißt der Betreiber diess Blogs mit Vornamen?
 
 
Nach oben