为什么我用brainf k编写了一个程序,为什么你也应该这样做

One day on a whim, I decided to write a program in a goofy language called “Brainf**k” (you can fill in the rest of the letters). Starting off as a joke, I realized that I was learning a lot more along the way. Learning Brainf**k has had a profound impact on my thinking as a software engineer, and I think everybody can benefit from learning it. The program I wrote can be seen below. If you want to know more about this amazing language, please read on.

有一天我心血来潮,我决定用一种愚蠢的语言编写一个程序,叫做“ Brainf ** k”(您可以填写其余的字母)。 从开玩笑开始,我意识到我在此过程中学到了很多东西。 学习Brainf ** k对我作为软件工程师的思想产生了深远的影响,我认为每个人都可以从学习中受益。 我写的程序可以在下面看到。 如果您想进一步了解这种神奇的语言,请继续阅读。

++++++++++>>>>++++++++++[>++++<-]>+++>++++++++++<<<<<<[[->+>+<<]>[-<+>]>[>>>.<<<-]>>>>.<<<<<<-]

目录: (Table of Contents:)

  1. Esoteric Languages

    神秘的语言

2. This Language is F***ed Up. But Really Cool…

2. 该语言已安装完毕。 但是真的很酷

3. Language Introduction

3. 语言介绍

4. Breaking Down a Program

4. 分解程序

5. Why the Language will Change You

5. 为什么语言会改变你

6. Challenge

6. 挑战

神秘的语言 (Esoteric Languages)

An esoteric programming language is a language created not with the hopes of boosting productivity or being the next C++ killer, but mainly “just because.” The “just because” part can be “because it’s funny”, “because it’s impossible to read”, “because it works”, or “because I want to piss people off.” Esoteric languages are more often than not a study of the asinine side of computer science, but sometimes they elicit enlightenment that cannot be found with typical languages.

神秘的编程语言不是为了提高生产率或成为下一个C ++杀手而创建的语言,而主要是“正因为如此”。 “仅因为”部分可以是“因为它很有趣”,“因为它不可能阅读”,“因为它有效”或“因为我想惹恼别人”。 神秘的语言通常不是对计算机科学的精明方面的研究,但有时它们会激发出典型语言无法找到的启发。

这种语言已经搞砸了。 但是真的很酷 (This Language is F***ed Up. But Really Cool…)

I picked up an esoteric language one day on a whim of sheer boredom-driven amusement seeking. The hunt for the right language was… interesting to say the least. There are languages that are driven by Shakespeare-inspired syntax, ones that only contain whitespace, and some that are designed to be impossible to read by the human eye. Then I stumbled upon the perfect language with a tantalizing name. BRAINF**K. I will maintain the asterisks throughout this story to keep things professional.

一天,我在无聊的娱乐驱动中产生了一种深奥的语言。 至少可以说,寻找正确的语言很有趣。 有些语言是由莎士比亚风格的语法驱动的,有些仅包含空格,有些则被设计为人眼无法阅读。 然后,我用一个诱人的名字偶然发现了完美的语言。 BRAINF ** K。 在整个故事中,我将保留星号以使内容保持专业。

Brainf**k is a very minimalist and Turing complete language. What does Turning complete mean? It means that a language is fully capable of solving arbitrary problems and can emulate a Turing machine. The language is considered to be “computationally universal.” All Turing complete languages can equally carry out the same computational process, albeit with different degrees of difficulty. For example, building a web browser in pure RISC-V assembly code is COMPLETELY POSSIBLE, but it would most definitely be more efficient to build it in Rust or C++. RISC-V assembly code, Rust, C++, Golang, COBOL, Visual Basic, Perl, Haskell, and most other mainstream programming languages are all Turing complete, meaning you can carry out the same process on any of them. The only differences are the syntax, architecture, and the amount of work that is needed to get the job done. Another important concept to understand regarding Turing completeness is the Turing machine.

Brainf ** k是一种非常简约的图灵完整语言。 车削完成是什么意思? 这意味着一种语言完全能够解决任意问题,并且可以模仿图灵机。 该语言被认为是“计算通用”。 所有图灵完整语言都可以平等地执行相同的计算过程,尽管难度有所不同。 例如,完全可以使用RISC-V汇编代码构建Web浏览器,但是绝对可以用Rust或C ++来构建Web浏览器。 RISC-V汇编代码,Rust,C ++,Golang,COBOL,Visual Basic,Perl,Haskell和大多数其他主流编程语言都是Turing完整的,这意味着您可以对它们中的任何一个执行相同的过程。 唯一的区别是语法,体系结构以及完成工作所需的工作量。 关于图灵完整性的另一个重要概念是图灵机。

A Turing machine is a theoretical computer that has a two basic components:

图灵机是一种理论计算机,具有两个基本组件:

  • An infinite “tape” divided into discrete cells that represent the memory of the computer

    无限的“磁带”分为离散的单元,代表计算机的内存
  • A scanning “head” that is capable of reading and writing symbols to the cells on the tape

    能够读取符号并将其写入磁带上单元的扫描“头”

With the above two components, it is theoretically possible to simulate ANY computational algorithm ever created, and that can be created, on a normal computer (don’t ask about quantum superposition). The process for how algorithms are carried out is as follows:

使用以上两个组件,从理论上讲,有可能在普通计算机上模拟曾经创建且可以创建的任何计算算法(不要问量子叠加)。 算法的执行过程如下:

  1. The “head” starts at the beginning of the “tape” and reads a symbol

    “ head”从“ tape”的开头开始,并读取符号
  2. Based on the symbol and the machine’s current state, the machine will either write a new symbol and move left or right a set number of cells, or just move a set number of cells

    根据符号和机器的当前状态,机器将编写一个新符号并向左或向右移动一定数量的单元格,或者仅移动一定数量的单元格
  3. The machine will then read the symbol at the location it moved to, and repeat the above process

    然后机器将在其移动的位置读取符号,并重复上述过程
  4. The machine continues to operate until it scans a symbol that tells the machine to halt

    机器继续运行,直到扫描到指示机器停止的符号为止

I have included a sample read/action cycle below from a 3-state “Busy Beaver” Turing Machine.

我在下面提供了一个三态“ Busy Beaver”图灵机的示例读取/操作周期。

Image for post
3 State “Busy Beaver” Turing Machine
三态“忙海狸”图灵机

With an idea so simple, it is possible to create infinitely many complex algorithms and programs. This is what makes the world of computer science a sandbox of unlimited creativity.

有了如此简单的想法,就有可能创建许多复杂的算法和程序。 这就是使计算机科学世界成为无限创造力的沙箱的原因。

Now let’s go onto BRAINF**K. So what makes BRAINF**K so F****D up? It is fully Turing complete because it is essentially a Turing machine. This language takes computer science back to its roots and requires developers to fully visualize and design their program as if given a set number of symbols and long tape (not infinite since computer memory is not infinite). Pretty nifty right? But still F***D up…

现在让我们进入BRAINF ** K。 那么,是什么使BRAINF ** K如此F **** D? 它完全是图灵完整的,因为它本质上是图灵机。 这种语言将计算机科学带回了根源,并要求开发人员完全可视化和设计其程序,就好像给定了一定数量的符号和长磁带(由于计算机内存不是无限的,所以不是无限的)。 很漂亮吧? 但是还是F *** D起来...

语言介绍 (Language Introduction)

Brainf**k is a pseudo Turing machine in that it operates on the principal of memory manipulation and does not operate on the typical states that a Turing machine would. The language was developed in 1993 by Urban Müller and is designed for extreme minimalism, and probably to drive programmers mad. Not sure on the last design goal, but it seems highly probable.

Brainf ** k是伪图灵机,因为它以内存操作的原理运行,而不以图灵机的典型状态运行。 该语言由UrbanMüller于1993年开发,旨在实现极简主义,并可能使程序员发疯。 不确定最后一个设计目标,但似乎很有可能。

The language only recognizes 8 characters:

该语言只能识别8个字符:

>    Increments the data pointer (moves the read/write head to the 
right)
< Decrements the data pointer (moves the read/write head to the
left)
+ Increments the byte at the data pointer
- Decrements the byte at the data pointer
. Outputs the value of the byte at the data pointer (In ASCII)
, Takes as input a byte from the user and stores it in the
current cell
[ If the current byte is 0, the program will jump past the next
"]" and continue (similar to breaking a loop), else it will
continue to the next command
] If the current byte is not 0, the program will jump back to
the command after the previous "[" (like a loop), else it will
continue to the next command

With this limited instruction set, pretty much any program can be built. The only limit is your patience, sanity, and perseverance.

使用此有限的指令集,几乎可以构建任何程序。 唯一的限制是您的耐心,理智和毅力。

分解程序 (Breaking Down a Program)

Let’s breakdown a very simple program. We will look at the addition of the numbers 3 and 7.

让我们分解一个非常简单的程序。 我们将看一下数字3和7的加法运算。

+++>+++++++[<+>-]<.
  1. The first 3 “+” will increment the value of cell 0 three times

    前三个“ +”将使单元格0的值增加三倍
  2. We then move to cell 1 with the “>” operator

    然后,我们使用“>”运算符移至单元格1
  3. We then use 7 “+” to increment cell 1 seven times

    然后,我们使用7“ +”将单元格1递增7次
  4. We are nonzero, so we start the continue to the next command after “[“

    我们为非零值,因此我们在“ [”之后开始继续下一个命令
  5. We shift to the left “<” and increment the first cell by 1

    我们向左移动“ <”,并将第一个单元格加1
  6. We then shift to the right “>” and decrement the second cell by 1

    然后,我们移至右侧的“>”并将第二个单元格减1
  7. Steps 5 and 6 are repeated until the second cell is 0 (breaking condition for loops in Brainf**k). We are essentially stealing a bit from the second cell and shifting it over to the first cell one-by-one until the second cell is empty. This is a highly atomic and iterative method of addition.

    重复步骤5和6,直到第二个单元格为0(Brainf ** k中循环的中断条件)。 我们实质上是从第二个单元中窃取了一点,然后将其一步一步转移到第一个单元中,直到第二个单元为空。 这是一种高度原子且迭代的加法。
  8. The final “<.” will shift back to the first cell and output the value, which is the addition of 3 and 7.

    最后一个“ <。” 将移回第一个单元格并输出值,该值是3和7的和。

Pretty simple right? Let’s move onto something a bit more complex:

很简单吧? 让我们继续进行一些复杂的工作:

++++++++++>>>>++++++++++[>++++<-]>+++>++++++++++<<<<<<[[->+>+<<]>[-<+>]>[>>>.<<<-]>>>>.<<<<<<-]

This program I built will output the quintessential introductory computer science looping pattern:

我构建的该程序将输出典型的计算机科学入门循环模式:

++++++++++
+++++++++
++++++++
+++++++
++++++
+++++
++++
+++
++
+

So how does it work? I leveraged some interesting memory management here by establishing constants, maintaining a decrementing counter, and performing some initial upfront computation to see how many “+” I would be outputting. Rather than provide a walkthrough of how this program works, feel free to copy the code into the following AWESOME tool (no affiliation to the creator). The link should automatically open the above program: https://fatiherikli.github.io/brainfuck-visualizer/#KysrKysrKysrKz4+Pj4rKysrKysrKysrWz4rKysrPC1dPisrKz4rKysrKysrKysrPDw8PDw8W1stPis+Kzw8XT5bLTwrPl0+Wz4+Pi48PDwtXT4+Pj4uPDw8PDw8LV0=

那么它是怎样工作的? 在这里,我通过建立常量,维护递减计数器并执行一些初始的前期计算来查看我将输出多少“ +”,从而利用了一些有趣的内存管理。 可以随意将代码复制到以下AWESOME工具中(而不是与创建者有隶属关系),而不是提供有关该程序如何工作的演练。 该链接应自动打开上面的程序: https : //fatiherikli.github.io/brainfuck-visualizer/#KysrKysrKysrKz4+Pj4rKysrKysrKysrWz4rKysrPC1dPisrKz4rKysrKysrKysrPDw8P4W4P4W4P4W8P4W4W4P4W4P8W4P4W8P4W8P4W1P0W4P8W1P0W1P

Be sure to adjust the delay so that you can see the initial computation where the program defines the length of the first row and how many “+” will be required. I also challenge you to adjust the program and see how it behaves. Break it, make it, twist it, put your own personal spin on it.

确保调整延迟,以便您可以看到初始计算,其中程序定义了第一行的长度以及需要多少“ +”。 我还挑战您调整程序,并查看其运行方式。 打破它,制造它,扭曲它,自己动手旋转。

为什么语言会改变你 (Why the Language will Change You)

After learning Brainf**k I gained a whole new respect for memory management and what low-level languages accomplish for us behind the scenes. Brainf**k forces you to think of variables in different ways, shows how the cursor can collide with memory cells you want to leave alone that end up breaking the entire program, and make you think of how efficient your programs are running behind the scenes. For example, arrays become increasingly complex in Brainf**k as you have to maintain the memory in a pristine contiguous fashion in order to operate on it.

学习了Brainf ** k之后,我对内存管理以及底层语言为我们带来的成就获得了全新的尊重。 Brainf ** k迫使您以不同的方式思考变量,显示光标如何与您想单独留下的存储单元碰撞,最终破坏整个程序,并使您想到程序在后台运行的效率如何。 例如,在Brainf ** k中,阵列变得越来越复杂,因为您必须以原始连续的方式维护内存才能对其进行操作。

I don’t necessarily even consider this language to be esoteric. No, it definitely would not be suitable to build an enterprise application in or build the next SaaS everybody is raving about, but it should be seen as a tool to unlock a different mode of thought when doing development. I definitely advocate for it getting taught in computer science programs to teach students basic memory management principles and the roots of computer science. If anything, it will make them appreciate what they have when writing linked lists in C++ :)

我什至不必认为这种语言是深奥的。 不,它绝对不适合在每个人都热衷的范围内构建企业应用程序或构建下一个SaaS,但应将其视为在开发时解锁不同思维方式的工具。 我绝对主张在计算机科学课程中教它,以教给学生基本的内存管理原理和计算机科学的根源。 如果有的话,这会让他们欣赏用C ++编写链表时所拥有的东西:)

挑战 (Challenge)

Write a program in Brainf**k, even if it is a simple one, and try it out in the visualizer. Be sure to share here! I would love to see what you come up with, and what you learned while writing it. How was it debugging the program? Did you run into any problems where you had to shift your cells all the way to the right and out of the way (I had to do this)? Did you give up? I would love to hear it!

即使是简单的程序,也可以用Brainf ** k编写,然后在可视化工具中试用。 一定要在这里分享! 我很想看看您的想法,以及在编写过程中所学到的知识。 如何调试程序? 您是否遇到了任何问题,必须将单元一直向右移开(我必须这样做)? 你放弃了吗 我很想听听!

I hope you learned something here today about the magnificent world of Brainf**k. I know every time I revisit the subject, I learn a little something extra and end up getting lost in thought.

希望您今天在这里学到了有关Brainf ** k宏伟世界的知识。 我知道每次重新学习该主题时,我都会学到一些额外的东西,最终会陷入沉思。

翻译自: https://medium.com/the-innovation/why-i-wrote-a-program-in-brainf-k-and-why-you-should-too-19a468c4c2ed

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值