tags:

views:

1705

answers:

13

I have seen examples of the Brainfuck language, but it's very strange and then I started thinking: What use does the language have?

+32  A: 

It's designed to be esoteric and to make people ask questions like this.

jbrennan
Briliant, a recursive existence, just like the universe. It is there so we can question why its's there ;-).
Gamecat
@Gamecat, well we wonder why the universe is here because we are here. Not sure about the other other way around.
instanceofTom
+40  A: 

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.

JaredPar
Just learnt something new, and I had completly the wrong idea about this. :-) I love it when that happens.
Reallyethical
If only people would know about Wikipedia ...
01
http://en.wikipedia.org/wiki/Wikipedia
Pavel Minaev
@01 - you don't get rep for wikipedia
Chad
+1  A: 

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! :-)

Reallyethical
+6  A: 

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.

sepp2k
+6  A: 

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.

Dana
Not to mention INTERCAL.
legoscia
Unlambda?? Luxury. It has 6 whole symbols plus character literals. Real men code in pure SK combinator calculus (just 3 symbols and no literals.)
finnw
+17  A: 

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.

legoscia
Yes, I once wrote an operating system and a C++ compiler in brainfuck.
shoosh
That is nothing. I once ported the linux kernal to a single tape turing machine.
Brian Ensink
Iran and North Korea are running their nuclear facilities solely on brainfuck.
Blessed Geek
@h2g2java: lol, very nice joke!
Nathan Campos
It's not the smallest; the smallest is a single instruction (http://en.wikipedia.org/wiki/One_instruction_set_computer) - "subtract and branch if negative" being the most common choice.
Pavel Minaev
@Pavel Minaev: thanks; edited accordingly.
legoscia
Arguably OISC is not the smallest language, because there are N^3 * M variants (where N is the number of registers and M is the size of the instruction address space.) SK combinator calculus (http://en.wikipedia.org/wiki/SK_combinator_calculus) requires only 3 symbols (`S` `K` and `\``.) Unlambda (http://www.madore.org/~david/programs/unlambda/) is a superset and has 4 extra symbols.
finnw
+1  A: 

To fuck up your brain..

is that the best you've got?
George Jempty
I was going to say to fuck with your brain, but close enough.
corymathews
@George Jempty, yep.
+8  A: 

It`s a Turing tarpit.

Paul Nathan
+147  A: 

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
avelldiroll
I wish I could up vote this more than once
Chris Porter
An interesting approach, but I wonder if the time investment (writing those BF solutions) is worth the brief moment of joy in the end ~
Pavel Minaev
@Pavel Minaev - as far as i remember it took me roughly 1/2 hour and as i suggested in my answer this was mainly an attempt to bore myself to sleep ... the sarcastic grin at the end is just a bonus.
avelldiroll
@avelldiroll: As as fellow insomniac this actually makes me want to learn brainf*ck.
Bob Dylan
@Pavel Minaev: it can be a mentally stimulating exercise trying to solve some simple problems in BrainFuck, so I would say, that the investment is worth it.
Martinho Fernandes
@Pavel - s/writing those BF solutions/Dinner. Movie. Flowers. Romantic talk. ... /;
DVK
My brain has just been fucked
Pierreten
ha, and i thought that regex was fucked up
Rodrigo
+3  A: 

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

  1. 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 as ooooooooooooooooooooooooooooooooooo$ - and have to count them for ourselves

  2. BF 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)

  3. 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!");
Nas Banov
A: 

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 :).

Marius Schulz
+6  A: 

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:

++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
Vivin Paliath
Wow. Is there a limit to how insane a programming language can be?
sharptooth
@sharptooth apparently not ;)
Vivin Paliath
This is brilliant :)
Anton
I did a quick google for cherryblosum but I couldn't find anything. (Your link is broken) Could you provide another link?
sixtyfootersdude
@sixtyfootersdude yeah, my website is down at the moment (upgrading my box) - I need to fix it. You can check it out at github: http://github.com/vivin/CherryBlossom
Vivin Paliath
+1  A: 

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.

Tom Gullen
Then, the key is very secure...
Nathan Campos