I have seen examples of the Brainfuck language, but it's very strange and then I started thinking: What use does the language have?
It's designed to be esoteric and to make people ask questions like this.
The wikipedia page on BrainF*ck is fairly informative on this matter.
Urban Müller created brainfuck in 1993 with the intention of designing a language which could be implemented with the smallest possible compiler, inspired by the 1024-byte compiler for the FALSE programming language. Several brainfuck compilers have been made smaller than 200 bytes. The classic distribution is Müller's version 2, containing a compiler for the Amiga, an interpreter, example programs, and a readme document.
Having just tried my 1st one, I have to say its to completely kill off any synapses you had left after working all day polishing off a bottle of red wine.
GREAT FUN! :-)
The use of brainf*ck is to demonstrate how simple (as in lack of syntax etc. - not as in easy to use) a language can be while still being turing complete.
To challenge yourself!
There are a bunch of languages -- Malboge, Brainf*ck, Whitespace, Unlambda -- where the goal is to make even simple coding tasks a brain-stretching challenge.
Brainfuck is one of the smallest imaginable languages that is Turing-complete. That is, it can be proven that anything that can be computed on the computers we know today can be computed with Brainfuck. This is a slightly mind-boggling but also purifying revelation.
Also, it is an fun language to write a compiler as small as possible for. If I recall correctly, my x86 assembly one was 250 bytes when compiled.
Brainf*ck is my tool of choice to answer simple homework programming questions that did not specify a programming language.
A great tool to have a perfectly viable answer more difficult to understand than the original question.
For example (real one):
Student on the wrong mailing list:
How to make a program do that:
ABBBC B B ABBBC
for: square (5,3)
In fact square contains 2 variables x and y (in the example x = 5 et y = 3) Can someone help me?
Me being a playful insomniac that day:
One should ask for direction, not ask for his/her homework to be done for him/her. And one should indicate which language he/she is using when asking about programming.
That being said here is an answer to your question in brainf*ck (plenty of interpreters available):
######rectangle.bf####### ++++++++++>>++++++++[<++++++++>-]<+>>++++++++[<++++++++>-] <++>>++++++++[<++++++++>-]<+++>>++++++++[<++++>-]<>, >++++++++[<------>-]<>,>++++++++[<------>-]< [>+>+<<-]>>[<<+>>-]<<<<<<<<.>.>>>>>>--[<<<<<.>>>>>-]<<<<.<<<. >>>>>--[>[>+>+<<-]>>[<<+>>-]<<<<<<.>>>>>--[<<<.>>>-]<<<<<.<<. >>>>>-]>[>+>+<<-]>>[<<+>>-]<<<<<<<<>.>>>>>>--[<<<<<.>>>>>-]<<<<.<<<. ########################
usage:
$ bf rectangle.bf 35 ABBBC B B ABBBC
As pointed out, Brainfuck is Turing-complete - that is, equal in abilities to said machine that looks up in a table and writes symbols on infinite tape. It's been shown that current computers - even the most powerful ones - are in theory nothing more than turbo-fast Turning machine.
However, Brainfuck is more pleasant/feasible language to write programs than Turing machine. (YES, i mean it! have your written anything non-trivial for T.M.?). Let's see
BF has number increment/decrement, so you can store 1 value per cell - in the case of Turing it is typical to represent number N as a string of some symbol with length N, so say if we want it to calculate 5*7, we will write
ooooo_ooooooo$
and after program run will will find the result on the tape asooooooooooooooooooooooooooooooooooo$
- and have to count them for ourselvesBF has I/O, so you get more of the feeling of real program than having to decode manually a memory dump (which Turing tape essentially is)
BF has [ ] loops that are easier to work with than the jump addresses table in Turing's case. You can nest them easily, don't have to worry about wrong addresses and having to count offsets.
Now what is Brainfuck useful for? Not for "production use", that is for sure. But it can be a great li'l language for educational purposes - in particular to write interpreter and/or compiler for.
In particular it can be fun learning how to do compiler optimizations. Let's take for example the following BF program that prints "Hello World' (it has to generate the ASCII codes of the leters in the process): ++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
A direct (cross)compilation to C may look something like that:
data[dp]++;
data[dp]++;
data[dp]++;
data[dp]++;
data[dp]++;
data[dp]++;
data[dp]++;
data[dp]++;
data[dp]++;
data[dp]++;
while (data[dp]) {
dp++;
data[dp]++;
data[dp]++;
data[dp]++;
data[dp]++;
data[dp]++;
data[dp]++;
data[dp]++;
dp++;
data[dp]++;
data[dp]++;
data[dp]++;
data[dp]++;
data[dp]++;
data[dp]++;
data[dp]++;
data[dp]++;
data[dp]++;
data[dp]++;
dp++;
data[dp]++;
data[dp]++;
data[dp]++;
dp++;
data[dp]++;
dp--;
dp--;
dp--;
dp--;
data[dp]--;
}
dp++;
data[dp]++;
data[dp]++;
putchar(data[dp]);
dp++;
data[dp]++;
putchar(data[dp]);
data[dp]++;
data[dp]++;
data[dp]++;
data[dp]++;
data[dp]++;
data[dp]++;
data[dp]++;
putchar(data[dp]);
putchar(data[dp]);
data[dp]++;
data[dp]++;
data[dp]++;
putchar(data[dp]);
dp++;
data[dp]++;
data[dp]++;
putchar(data[dp]);
dp--;
dp--;
data[dp]++;
data[dp]++;
data[dp]++;
data[dp]++;
data[dp]++;
data[dp]++;
data[dp]++;
data[dp]++;
data[dp]++;
data[dp]++;
data[dp]++;
data[dp]++;
data[dp]++;
data[dp]++;
data[dp]++;
putchar(data[dp]);
dp++;
putchar(data[dp]);
data[dp]++;
data[dp]++;
data[dp]++;
putchar(data[dp]);
data[dp]--;
data[dp]--;
data[dp]--;
data[dp]--;
data[dp]--;
data[dp]--;
putchar(data[dp]);
data[dp]--;
data[dp]--;
data[dp]--;
data[dp]--;
data[dp]--;
data[dp]--;
data[dp]--;
data[dp]--;
putchar(data[dp]);
dp++;
data[dp]++;
putchar(data[dp]);
dp++;
putchar(data[dp]);
Which is good and fine and works. But the code can also be optimized (http://www.iwriteiam.nl/Ha_bf_online.html) to
p[0] += 10;
p[1] += 7 * p[0];
p[2] += 10 * p[0];
p[3] += 3 * p[0];
p[4] += p[0];
p[0] = 0;
p[1] += 2;
putchar(p[1]);
p[2]++;
putchar(p[2]);
p[2] += 7;
putchar(p[2]);
putchar(p[2]);
p[2] += 3;
putchar(p[2]);
p[3] += 2;
putchar(p[3]);
p[1] += 15;
putchar(p[1]);
putchar(p[2]);
p[2] += 3;
putchar(p[2]);
p[2] -= 6;
putchar(p[2]);
p[2] -= 8;
putchar(p[2]);
p[3]++;
putchar(p[3]);
putchar(p[4]);
Or if your optimizer is really tough (http://code.google.com/p/esotope-bfc/wiki/Optimization), you can end up with something like
p[1] = 0;
p[0] = 100;
p[3] = 0;
p[2] = 33;
p[5] = 0;
p[4] = 87;
puts("Hello World!");
Brainfuck has no real use as it's an esoteric programming language. However, it's quite fun to write programs for small problems and then see those solved with Brainfuck :).
Shameless plug follows:
Don't use Brainf*ck, use its prettier analog: CherryBlossom! You program with haikus and the resulting program is aesthetically pleasing.
In fact, here is "Hello World!" in CherryBlossom:
beautiful jasmine
your lovely fragrance heals me
every morning
remembering you,
dreaming of your lovely smile,
when will you come here?
floating butterflies
sunshine and summer flowers
a lovely morning
blossoming hillside
on a fragrant summer day
blooming, flowering.
I can remember
my happy dreams of summer
it was beautiful
flying doves, sunrays
beauty flying in sunshine
rain in the valley.
snow falls in moonlight,
returns to the mountainside.
lovely, beautiful.
view from mountaintop
is a beautiful painting,
in summer sunshine.
the fragrant flowers
and the pretty butterflies
spring by singing creek.
beautiful morning
butterflies by riverside
floating in sunshine.
such a lovely sight,
the valley waterfall is
in the spring sunshine.
sunrays and sunshine,
the butterflies and flowers
loving the new spring.
the pretty flowers
are dreaming of a summer
with the smiling sun.
music from heaven,
is melodious and sweet,
dreamy and happy.
the river is cold
and misty in the moonlight,
in the autumn chill.
winter riverside,
lonely, icy, and chilly
darkening evening
the lonely winter,
barren riverside ahead
a dreaming poet
That's a lot prettier than:
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
It is said that BrainFuck
holds the secret key to the kingdom of Persia
. BrainFuck
was adopted from the streets of Nasaf by King Sharaman, and BrainFuck
grew up in royalty. BrainFuck's
brothers, Garsiv and Tus plan battle strategies, a spy sends word that the Holy City of Alamut has been supplying weapons to enemies of Persia.
Taking matters into his own hands, Tus orders an attack on the sacred city and upon its fall BrainFuck
encounters the beautiful Princess Tamina
. When King Sharaman dies under mysterious circumstances shortly after, and BrainFuck
is accused of his murder, he flees with the princess on a harrowing mission to clear his name and protect the secret key to the kingdom of Persia
.