Saturday, March 26, 2011

How About a BrainF**k session: Esolangs


Recently I was reading this article about Esoteric Programming Languages, shortened form: esolangs.


Esolangs are generally created out of curiosity, for fun, sometime to test boundaries of computer programming but mostly they have very little practical significance, as they are not created keeping any particular problem in mind.
Yet they are interesting in a way that you don't have to put much brain to understand their semantics(most of the time but not for all). They are easy to learn hard to write, that give you code-writing challenges. There are many such languages exist, such as whitespaces, brainfuck, chef, lolcode, intercal etc.

So, last sunday I gave a try to learn and test one of them(which was BrainFuck). As its neither a complete-lost language nor a practically feasible one. Primary reason for choosing BrainFuck was reasonably simple though: the name itself (:D). But as I started writing code in it, I found it really challenging even to output a simple string.


Since the term itself seems to be derogatory, I will call it #BF from here on. 

#BF was invented by Urban Müller, whose goal was to create the smallest possible compiler, which is 171 bytes in size (as of now). The language has only 8 operators that are all written as a single character. The 8 operators are: + - < > [ ] . , 
  • + increase byte under pointer
  • - decrease byte under pointer
  • > increase pointer
  • < decrease pointer
  • [ start a loop if byte under pointer is not 0
  • ] return to matching [
  • . print byte under pointer to standard output (often screen)
  • , read input from standard input (often keyboard) to byte under pointer
It uses a simple machine model consisting of the program and instruction pointer, as well as an array of at least 30,000 byte cells initialized to zero; a movable data pointer (initialized to point to the leftmost byte of the array); and two streams of bytes for input and output (most often connected to a keyboard and a monitor respectively, and using the ASCII character encoding).
 

#BF heavily depends on your understanding of data-pointers.


A simple string like 'MUKESH' can be written as:


++++++++++      initialize counter (cell #0) to 10
[               use loop to set the next four cells to 70/80
> +++++ ++      add  7 to cell #1
> +++++ +++     add  8 to cell #2
<<-             decrement counter (cell #0)
]
>>---.          print 'M' ASCII value: 77 i.e pointer moves 3 step behind cell#2
++++++++.       print 'U'
ASCII value: 85 i.e. pointer moves 8 step ahead
<+++++.         print 'K'
ASCII value: 75 i.e. pointer steps back to cell#1
------.         print 'E'
ASCII value: 69 i.e. pointer moves 6 step behind
>--.            print 'S'
ASCII value: 83 and so on...
<+++.           print 'H'
ASCII value: 72



And now after this small but succinct session on #BF, here is a simple program for you to scratch your head :P.


//Starts here
+++++ +++++
[
> +++
> +++++ ++
> +++++ +++
> +++++ ++++
> +++++ +++++
> +++++ +++++ +
<<<<<<-
]
>>+.
>>>>+.
.
<.
<<<<+++.
-.
>>>>>+++++ +++++.
----- -----.
+++++ +.
<<<<<.
>>>>-.
>---.
<--.
++.
>----- --.
<<<<<.
>>>>+++++ +.
>+++++ ++++.
<<<<<+++++ +++++ ++++.
----- ----- ----.
>>--.
>>>-----.
+++++ +++.
<<<<<.
+++++ +++++.
.
.
.
----- -----.
>>>>>----- ---.
<---.
.
<<<<+.
.
.
//Ends here


Note: [Part of it is taken from wiki and other references. Code snippets are mine though. Enjoy reading and please give your valuable suggestions on improvement of further posts. Thanks :)]

Howdy!

OK!!!
So its a long time I was planning to start my tech-blog, but every-time I hit the 'create blog' button, i realizes that technical discussions are not exactly my cup of tea. that I will not call myself a Geeky or a Dorky guy, I am more of technically proactive person than a technically gifted one.

Finally I zeroed-in to a kind of blog which is certainly not for the nerds, but for people who just want to stay aware of newly introduced technologies, may be devices that entered in market, want to have fun with coding and so on, but not...not for people who want answer to some creepy technical problem.

Writings will be more often on varied topics irrespective of technical or philosophical biasing(or may be a little biasing towards tech part :) ) but the overall idea behind this blog is just to have fun, pass some luxury time or just to get technically literate for one or few things.

Its about learning technology in a layman's language and for those people who web-surf just to imbibe anything new that they hit upon.

Henceforth enjoy the blog. I hope, that I could make it an active blog with timely additions of new posts.