Skip to content

Playing God with Scheme

Pretty sensitive issue this – whether man should be allowed to play God or not. I was a debater once, and I’ve pretty much debated this with dozens of arguments from each side. One thing you can’t argue with, though: playing God is fun.

I was playing around with Scheme today (for the uninitiated and/or the young, that is a programming language). I was pretty uninitiated too, which is why I had never bothered to try out Scheme before… but this is the closest to playing God that I have come. Here’s why.

Weird but cool syntax: Almost everything in scheme feels like it must have been created around the beginning of time. Scheme is to C++ what Sanskrit is to Hindi. For instance, here’s how you add two numbers in Scheme:

(+ 2 3)
5

This seems simple enough to follow at first – but then you realize everything in Scheme follows this rule – and so if you want to write an absolute value function, this is how you do it:

(define (abs x) (if (< x 0) (- x) x))

It all goes downhill from there.

Warp the world: The scheme book I was reading unwisely told me that anything and everything in Scheme was changeable. I did not read a sentence further and went ahead and wrote this out:

(define (+ a b) (* a b))
Setting compiled read-only variable+ can yield to incoherent state
+

This basically tells the interpreter that I want addition to mean multiplication. Despite the warning that I was entering a demented world, I was overjoyed that this appeared to work. And, sure enough:

(+ 5 10)
50

Bending the fabric of reality: Upward to enlightenment, I realized that + was just a name, and could really point to anything. We often hear these days that "everything is an object". But really, in Scheme, "everything is what you want it to be". And so, after turning my world upside-down, I turned it inside out.

(define + 2)
Setting compiled read-only variable+ can yield to incoherent state
+

Which says I want + to be a variable with value 2. For an ideal God, of course, the world would just carry out orders without cursing, but hey, I was just playing God! And was indeed bending the world to my will. And so I can now enter this, which looks positively alien, but is perfectly logical and returns a nice round figure:

(* + (- + +) (/ (* + +) +))
0

By this time, of course, subjects of the now-warped-and-bent-beyond-recognition world were, as you can imagine, feeling like Neanderthal man presented with the supernova of the Crab Nebula (or bash users presented with the Windows command line), and I thought it was about time to un-warp the world again. Even without restarting the interpreter, this is quite easily done:

(define (+ a b) (- a (- b)))

...and there was light :D (though of course the original + procedure can take any number of arguments, I'll figure that out someday)

If you're feeling down, I heartily recommend an episode of playing God to get you feeling on top of the world.

5 Comments