Learn to Code via Tutorials on Repl.it

← Back to all posts
Becoming a better problem solver through BrainF**k Part 2: Why? How?

# See all 4 tutorials

BrainF**k is cool and all, but why is it important to learn it? While one may not use BrainF**k skills in their daily life, using BrainF**k expands your brain by forcing you to look at things through a very limited lens. Much in the way that great art is created when artists have strict limits, great solutions to problems are created when there's a limited scope.

So, what problems should you look at when you use BrainF**k? Here's one program that has a very efficient solution in BrainF**k: reverse the input

``````> move forward(comma) so that the first cell is empty
, take input
[ while the input is nonzero
> move forward
, take another character of input
] the loop will exit once the input runs out
< go back to the last character of the input
[ while the current cell is nonzero (this is why the first cell is blank)
. print the current cell
< go backwards
]``````

Made shorter, this looks like: `>,[>,]<[.<]`, which is extremely short! It's even shorter than the JavaScript code for it, `e=>e.reverse()`

As programmers used BrainF**k in different silly challenges, they noticed that you can use certain tricks and patterns as sometimes the same thing comes up in many different programs. A few of these were present in the code above, but here are some more:

codetrick
`,[>,]`This trick puts the whole input into a strip of cells
`[.>]`This trick outputs everything until it reaches a zero
`,[ code ,]`this runs the same code on every single character of input
`> x plusses [ < y plusses > - ] <`Assuming the next cell is zero, this adds `x*y` to the current cell
`[-]`This makes any cell zero
`[>]` or `[<]`This moves along the memory until it finds an empty cell

These are just a few of the many tricks. There is a wiki for esotaric languages, such as BrainF**k that goes into detail about the language, including a bunch of these tricks, algorithms, and how the language interpreter works. It's an excellent resource, I'd recommend that you check it out.

With that, this tutorial comes to a close. Next tutorial, we'll finally get to a practical example and run through how it works. For now, I'll give you a practice challenge: write a BrainF**k program that, given a list of ONLY LOWERCASE CHARACTERS, makes every OTHER character uppercase. This challenge can be solved with the tricks learned today, but also requires the use of the ASCII table in the first tutorial. Good luck!