Monday, May 22, 2006

Visual Basic Self-Replicator

I am reading Introduction to the Theory of Computation. In the chapter about advanced topics in computability theory, it mentioned a technique for building a self-replicating Turing machine (a Turing machine that writes its own description on its tape). I have implemented this basic technique to write a self-replicating program using Visual Basic 6 (a program that outputs its own code). [In the form designer, create one TextBox called Text1 and set its Multiline property to True.]

Private Sub Form_Load(): Text1.Text = "Private Sub X(): Text1.Text = ""Private Sub Form_Load(): Text1.Text = """""" + Replace(Text1.Text, """""""", """""""""""") + """""": X: End Sub"" + vbCrLf + vbCrLf + Text1.Text: End Sub": X: End Sub

Private Sub X(): Text1.Text = "Private Sub Form_Load(): Text1.Text = """ + Replace(Text1.Text, """", """""") + """: X: End Sub" + vbCrLf + vbCrLf + Text1.Text: End Sub

Here is a rough overview of how it works. X() is just a subroutine to take the contents of Text1, and output the code for a Form_Load function that would write those contents to Text1, followed by a blank line, followed by the previous contents of Text1. Form_Load() is the block that executes first. It simply transcribes the code of X() into Text1, then calls X(). I have a hard time keeping this all in my head at once.

This took me an hour to write, and had many more complications than I expected. It was difficult to accurately reproduce the double-quote character (") as it is used in the code. The syntax for escaping the double-quote in a string literal is to write two consecutive double quotes (""). Notice that at one point, I have written twelve consecutive double-quote characters. That was probably the highlight.

Friday, May 19, 2006

Whistling

Here is another dorm observation: a lot of people in my dorm whistle a lot. They whistle loudly and for a long time. But there is no tune! They are whistling musical gibberish. And it's pretty annoying. What the hell?

Thursday, May 18, 2006

Quadratic Formula

ax^2 + bx + c = 0
Solve for x.

Obviously, x is equal to the quadratic formula. Can you derive it? Hint: the most important technique is completing the square.

The quadratic formula is fundamental for even the most basic algebra, and it only takes five or ten steps to derive. I think it should be on any high school algebra final exam.

Wednesday, May 17, 2006

Hair Dryers

I hate hair dryers so much! They make a terribly loud and annoying noise for about two minutes straight, and piss off dorm-mates like me. The point of all that is drying your hair? Are you kidding me? You can dry your hair with a towel. It's fine, really. Why do these things sell? A hair dryer is basically a rube goldberg for drying your hair.

Some asshole in my hall has been drying his or her hair since before I started writing this post. Fucking unbelievable.

Tuesday, May 16, 2006

Saturday, May 06, 2006

Two ears and one mouth

"Listening is more important than talking. That is why God has given us two ears and only one mouth."

So what's the alternative, one ear and two mouths? It makes sense that two ears were naturally selected because they allow our brain to triangulate the location of a sound source. There is no significant advantage a second mouth would offer that would compensate for the complexity of maintaining it.

I don't think it should be socially acceptable to spout those stupid cliches about how your anatomy supposedly corresponds to God's will. What kind of a sick joke would that be?