Last week, during the presentation at Waltz, Shoma-san mentioned about brainf*ck, which reminded me about one of the contest which I participated that allowed only this language to be used. So I decided to write a blog on this language.
Brainf*ck is one of the most famous esoteric programming languages (esoteric means one that is invented just for fun or experiment, and has no special significance or practical use). Because of the offensive word used in latter part of the name, it is a bit difficult to find information about this on the internet, and so it is referred by several names like brainf***, brainf*ck or simply BF. It was developed by Urban Muller in 1993 because he wanted to write a smallest possible compiler for the Amiga OS.
Brainf*ck takes the Turing machine model, and it operates on a array of memory cell, referred to as a tape. Each cell is initially set to 0, and the pointer points to a first memory cell. There are only a few commands that the language recognizes, which is shown below:
.
So lets try and write a the most famous program, Hello World! in brainf*ck. So here is the code to print Hello World! followed by a endline character.
1 |
++++++++++[>+++++++>++++++++++>+++>+<<<++.>+.+++++++..+++.>++.<.+++.------.--------.>+.>. |
The next code is to move the value at cell0 to cell1, here is the brainf*ck code:
1 |
[->+<] |
Explaination
I will explain this code because it is smaller. At first the pointer points to cell0. The ‘[‘ operator checks if the content of the current cell is zero or not, if zero it moves to the matching ‘]’ which here is the end of the code. In case non-zero, it decrements the current cell by a ‘-‘, moves to the next cell by a ‘>’, and increments that cell1 by a ‘+’. It then checks with a ‘]’ if the cell0 is zero or not, and if non-zero, it moves to the matching ‘[‘ which is the start of the code here.
I would like the readers to try and find out why Hello World! codes work. If anyone faces any problem in it, please feel free to contact me, or add a comment.
最近のコメント